TypeScript
Record
Record
用来给键值对类型的对象添加某种限制,比如说:
1
| const map: Record<string, Function> = {}
|
意思是这个对象以后必须键是 string
,值是一个函数,比如说:
这个可以。
但是这个则会报错:
Example:
1 2 3
| type LanPackage = Record<string, Record<string, string>> let aa:LanPackage = {"ajiao":{"test":"aaaa"}} let bb:LanPackage = {"ajiao":2}
|
as const
https://segmentfault.com/a/1190000043183214
1 2 3 4 5 6 7 8 9 10 11 12
| type Fruit = "Orange" | "Apple" | "Banana"
myString:string = "Banana";
myFruit:Fruit = myString;
1. myString:string = "Banana" as const; 2. myString:string = "Banana" as Fruit ; 3. myFruit: Fruit = myString as Fruit; 4. myString:string = <const> 'Banana';
|
Omit
作用是将前面参数中后面的属性过滤掉
1 2 3 4 5 6 7 8 9 10 11 12 13
| interface IUser { name: string; age?: number; class?: string; sex: string; }
type IOF = Omit<IUser, 'sex'> let ff: IOF = { name: '4', age: 4, class: '4', }
|
Keyof
官方:
1 2 3 4 5
| interface Person {name: string;age: number;} let personProps: keyof Person;
|
枚举中的用法:
https://www.jianshu.com/p/cd5dee829cb1
type中的用法:
https://blog.csdn.net/lcl130/article/details/125214788
Typeof
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| const a = {"ajiao": "1","test":2} type test = typeof a
type test = { ajiao: string; test: number; }
const a = 123 type test = typeof a
const a = "123" type test = typeof a
const a = ()=>{return {"ajiao":"123"}} type test = typeof a
type test = keyof type a
type test = "ajiao" | "test"
type test = `${a}`
|
1 2 3 4 5 6 7 8 9 10 11 12
| export enum Test { "a1" = "a2", "a2" = 12, "a3" = "45" }
type StrToNum<T> = T extends `${infer Num extends number}` ? Num : T
type StrTest = StrToNum<`${Test}`>;
|
ReturnType
官方解释:
获取函数返回值的类型
1 2 3 4 5 6
| function getUser() { return {name: 'xxx', age: 10} }
type GetUserType = typeof getUser; type ReturnUser = ReturnType<GetUserType>
|
https://www.typescriptlang.org/zh/docs/handbook/2/conditional-types.html#distributive-conditional-types
当从具有多个调用签名的类型(如重载函数的类型)进行推断时,将从 最后一个 签名进行推断(这也许是最宽松的万能情况)。无法基于参数类型列表执行重载决议。
1 2 3 4 5
| declare function stringOrNum(x: string): number; declare function stringOrNum(x: number): string; declare function stringOrNum(x: string | number): string | number; type T1 = ReturnType<typeof stringOrNum>;
|
Pick(选取)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| type Pick<T, K extends keyof T> = { [key in k]: T[key]} interface User { uid : number; username : string; password : string; email : string; residence : string; job : string; sex : number; birthday : string; } type RegisterArgs = Pick<User, 'username' | 'email' | 'password'> interface RegisterArgs { username : User['username']; email : User['email']; password : User['password']; }
|
Partial
Partial 将属性转为可省略
1
| type Partial<T> = { [P in keyof T]?: T[P] };
|
上面语句的意思是 keyof T
拿到 T
所有属性名,然后 in
进行遍历,将值赋给 P,最后 T[P]
取得相应属性的值,此时是都转为了省略
1 2 3 4 5 6 7 8 9 10 11 12
| interface User { id: number; age: number; name: string; }; type PartialUser = { id?: number; age?: number; name?: string; } type PartialUser = Partial<User>
|
in
in
则可以遍历枚举类型
1 2
| type Keys = "a" | "b" type Obj = {[p in Keys]: any}
|
infer