1、交叉类型 &
type LeftType = {
id: number;
left: string;
};
type RightType = {
id: number;
right: string;
};
type IntersectionType = LeftType & RightType;
// Output: {id: 1, left: "test", right: "test"}
2、联合类型 'A' | 'B'
3、extends A extends B => A 是 B的子集
4、Partial => 变成可选
5、Required => 变成必须
6、Readonly => 变成只读
7、Pick<T, K> => T里面提取K
8、 Omit => T里面删除K 和pick相反
9、Extract<T, U> => T和U 取交集
10、Exclude<T, U> => 从 T 中剔除可以赋值给 U 的类型,取差集
11、NonNullable<T> => 从 T 中剔除 null 和 undefined
12、in 类型保护
function showType(arg: FirstType | SecondType) {
if ('x' in arg) {
console.log(`The property ${arg.x} exists`);
return `The property ${arg.x} exists`;
}
throw new Error('This type is not expected');
}
13、record 他会将一个类型的所有属性值都映射到另一个类型上并创造一个新的类型
type petsGroup = 'dog' | 'cat' | 'fish';
interface IPetInfo {
name:string,
age:number,
}
type IPets = Record<petsGroup, IPetInfo>;
const animalsInfo:IPets = {
dog:{
name:'dogName',
age:2
},
cat:{
name:'catName',
age:3
},
fish:{
name:'fishName',
age:5
}
}