能够使用 type 和 interface 来建立类型函数
声明基本类型别名,联合类型,元组等类型code
type S = string; type IFoo = IBar | string;
可以使用 typeof 获取实例的类型赋值对象
const a:number = 1; const IA = typeof a; // IA 被 ts 识别为 number
interface 可以声明合并接口
interface IFoo { name:string } interface IFoo { age:number } // 等于 type IFoo = { name:string age:number }
以IFoo做为例子get
interface IFoo { name:string age:number gender:string }
type IBar = IFoo['name'] // IBar = string
type IBar = Pick<IFoo, 'name'> // IBar = {name: string}
type IBar = Pick<IFoo, 'name' | 'age'> // IBar = {name: string, age: number}
type IBar = Omit<IFoo, 'name'> // IBar = {age: number, gender: string}
type IBar = keyof IFoo // IBar = "name" | "age" | "gender"
type IBar = IFoo[keyof IFoo] // IBar = string | number
type IBar = Record<'name' | 'age', string> // IBar = {name: string, age: string}
interface IFoo { name: string age: string gender: string getSkill(): void setSkill: (skill: string[]) => void } // 生成一个新类型,将 age 和 gender 的类型修改成 number,其余的类型不变 // 使用上述知识 声明一个新的高级类型IBar: type IBar<K extends string,T = number> = (Record<K, T> & Omit<IFoo, K>) type IBaz = IBar<'age' | 'gender'> // 生成新的类型 IBaz ,符合上述描述 // 而且使用 Ibar 可将 age 和 gender (或其余)更改成任意其余类型 如: type IBax = IBar<'age' | 'gender' | 'name', string[]>
interface IFoo { name: string age: number gender: string getSkill(): void // type 不支持此种声明 setSkill: (skill: string[]) => void }
以此为例子:string
type IFoo = (name: string, age: number) => { name: string, age: number, gender: string }
type IBar = Parameters<IFoo> // IBar = [string, number]
type IBar = ReturnType<IFoo> // IBar = {name: string, age: number, gender: string}