TypeScript

TypeScript

Record

Record 用来给键值对类型的对象添加某种限制,比如说:

1
const map: Record<string, Function> = {}

意思是这个对象以后必须键是 string,值是一个函数,比如说:

1
map.a = () => { }

这个可以。

但是这个则会报错:

1
map.a = 1

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; //此时会报错
//Type 'string' is not assignable to type '"Orange" | "Apple" | "Banana"'

//使用以下方法都能解决
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
//keyof T,索引类型查询操作符。 对于任何类型T,keyof T的结果为T上已知的公共属性名的联合。 例如:
interface Person {name: string;age: number;}
let personProps: keyof Person; // 'name' | 'age'
//keyof Person是完全可以与'name' | 'age'互相替换的。
//不同的是如果你添加了其它的属性到Person,例如address: string,那么keyof Person会自动变为'name' | 'age' | 'address'。

枚举中的用法:

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
//得到的test是:
type test = {
ajiao: string;
test: number;
}

const a = 123
type test = typeof a //type test = 123

const a = "123"
type test = typeof a //type test = "123"

const a = ()=>{return {"ajiao":"123"}}
type test = typeof a //type test = () => { ajiao: string;}


//配合keyof:
type test = keyof type a
//得到的test是:
type test = "ajiao" | "test"

//获取value作为新的type
type test = `${a}` // type test = "1" |"2" //注意:改变了test的类型


1
2
3
4
5
6
7
8
9
10
11
12
//想要获取不改变类型的type
export enum Test {
"a1" = "a2",
"a2" = 12,
"a3" = "45"
}

type StrToNum<T> = T extends `${infer Num extends number}` ? Num : T

type StrTest = StrToNum<`${Test}`>; //type StrTest = "a2" | 12 | 45
// !!!!此方法又把string的45变成了number

ReturnType

官方解释:

获取函数返回值的类型

1
2
3
4
5
6
function getUser() {
return {name: 'xxx', age: 10}
}

type GetUserType = typeof getUser; //type GetUserType = () => {name: string;age: number;}
type ReturnUser = ReturnType<GetUserType> //type ReturnUser = {name: string;age: number;}

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>;//type T1 = string | number

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; // 用户ID
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} // -> { a: any, b: any }

infer


TypeScript
http://example.com/2023/02/20/TypeScript/
作者
John Doe
发布于
2023年2月20日
许可协议