TypeScript内置类型
TypeScript具有类型系统,且是JavaScript的超集,其可以编译成普通的JavaScript代码,也就是说,其是带有类型检查的JavaScript。
内置类型
TypeScript提供了几种实用程序类型来促进常见的类型转换,这些类型在全局范围内可用。
Partial
Partial<Type>构造一个类型使Type的所有属性都设置为可选。
/**
* Make all properties in T optional
*/
type Partial<T> = {
[P in keyof T]?: T[P];
};2
3
4
5
6
7
8
9
interface Example {
a: string;
b: number;
}
type PartialExample = Partial<Example>;
/**
* PartialExample
* interface {
* a?: string | undefined;
* b?: number | undefined;
* }
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Required
Required<Type>构造一个类型使Type的所有属性都设置为required,与Partial<Type>功能相反。
/**
* Make all properties in T required
*/
type Required<T> = {
[P in keyof T]-?: T[P];
};
interface Example {
a?: string;
b?: number;
}
type RequiredExample = Required<Example>;
/**
* RequiredExample
* interface {
* a: string;
* b: number;
* }
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Readonly
Required<Type>构造一个类型使Type的所有属性都设置为readonly,这意味着构造类型的属性都是只读的,不能被修改,这对使用Object.freeze()方法的对象非常有用。
/**
* Make all properties in T readonly
*/
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
interface Example {
a: string;
b: number;
}
type ReadonlyExample = Readonly<Example>;
/**
* ReadonlyExample
* interface {
* readonly a: string;
* readonly b: number;
* }
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Record
Record<Keys, Type>构造一个对象类型,其属性键为Keys,其属性值为Type,通常可以使用Record来表示一个对象。
/**
* Construct a type with a set of properties K of type T
*/
type Record<K extends keyof any, T> = {
[P in K]: T;
};
type RecordType = Record<string, string|number>;
const recordExample: RecordType ={
a: 1,
b: "1"
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Pick
Pick<Type, Keys>通过从Type中选择一组属性Keys来构造一个类型。
/**
* From T, pick a set of properties whose keys are in the union K
*/
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
interface Example {
a: string;
b: number;
c: symbol;
}
type PickExample = Pick<Example, "a"|"b">;
/**
* PickExample
* interface {
* a: string;
* b: number;
* }
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Omit
Omit<Type, Keys>通过从Type中选择所有属性然后删除Keys来构造一个类型,与Pick<Type, Keys>功能相反。
/**
* Construct a type with the properties of T except for those in type K.
*/
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
interface Example {
a: string;
b: number;
c: symbol;
}
type OmitExample = Omit<Example, "a"|"b">;
/**
* OmitExample
* interface {
* c: symbol;
* }
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Exclude
Exclude<UnionType, ExcludedMembers>通过从UnionType中排除可分配给ExcludedMembers的所有联合成员来构造类型。
/**
* Exclude from T those types that are assignable to U
*/
type Exclude<T, U> = T extends U ? never : T;
type ExcludeExample = Exclude<"a"|"b"|"c"|"z", "a"|"b"|"d">;
/**
* ExcludeExample
* "c" | "z"
*/2
3
4
5
6
7
8
9
10
11
12
13
14
Extract
Extract<Type, Union>通过从Type中提取所有可分配给Union的联合成员来构造一个类型,与Exclude<UnionType, ExcludedMembers>功能相反。
/**
* Extract from T those types that are assignable to U
*/
type Extract<T, U> = T extends U ? T : never;
type ExtractExample = Extract<"a"|"b"|"c"|"z", "a"|"b"|"d">;
/**
* ExtractExample
* "a" | "b"
*/2
3
4
5
6
7
8
9
10
11
12
13
NonNullable
NonNullable<Type>通过从Type中排除null和undefined来构造一个类型。
/**
* Exclude null and undefined from T
*/
type NonNullable<T> = T extends null | undefined ? never : T;
type NonNullableExample = NonNullable<number|string|null|undefined>;
/**
* NonNullableExample
* string | number
*/2
3
4
5
6
7
8
9
10
11
12
13
14
Parameters
Parameters<Type>从函数类型Type的参数中使用的类型构造元组类型。
/**
* Obtain the parameters of a function type in a tuple
*/
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
type FnType = (a1: number, a2: string) => void;
type ParametersExample = Parameters<FnType>;
/**
* ParametersExample
* [a1: number, a2: string]
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ConstructorParameters
ConstructorParameters<Type>从构造函数类型的类型构造元组或数组类型,其产生一个包含所有参数类型的元组类型。
/**
* Obtain the parameters of a constructor function type in a tuple
*/
type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;
interface Example{
fn(a: string): string;
}
interface ExampleConstructor{
new(a: string, b: number): Example;
}
declare const Example: ExampleConstructor;
type ConstructorParametersExample = ConstructorParameters<ExampleConstructor>;
/**
* ConstructorParametersExample
* [a: string, b: number]
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ReturnType
ReturnType<Type>构造一个由函数Type的返回类型组成的类型。
/**
* Obtain the return type of a function type
*/
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
type FnType = (a1: number, a2: string) => string | number;
type ReturnTypeExample = ReturnType<FnType>;
/**
* ReturnTypeExample
* string | number
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
InstanceType
InstanceType<Type>构造一个由Type中构造函数的实例类型组成的类型。
/**
* Obtain the return type of a constructor function type
*/
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
interface Example{
fn(a: string): string;
}
interface ExampleConstructor{
new(a: string, b: number): Example;
}
declare const Example: ExampleConstructor;
type InstanceTypeExample = InstanceType<ExampleConstructor>;
// const a: InstanceTypeExample = new Example("a", 1); // new ExampleConstructor => Example
/**
* InstanceTypeExample
* Example
*/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
ThisParameterType
ThisParameterType<Type>提取函数类型的this参数的类型,如果函数类型没有this参数,则为unknown。
/**
* Extracts the type of the 'this' parameter of a function type, or 'unknown' if the function type has no 'this' parameter.
*/
type ThisParameterType<T> = T extends (this: infer U, ...args: any[]) => any ? U : unknown;
function toHex(this: Number) {
return this.toString(16);
}
type ThisParameterTypeExample = ThisParameterType<typeof toHex>;
console.log(toHex.apply(27)); // 1b
/**
* ThisParameterTypeExample
* Number
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
OmitThisParameter
OmitThisParameter<Type>从Type中移除this参数,如果Type没有显式声明此参数,则结果只是Type,否则,从Type创建一个不带此参数的新函数类型。泛型被删除,只有最后一个重载签名被传播到新的函数类型中。
/**
* Removes the 'this' parameter from a function type.
*/
type OmitThisParameter<T> = unknown extends ThisParameterType<T> ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T;
function toHex(this: Number) {
return this.toString(16);
}
type OmitThisParameterExample = OmitThisParameter<typeof toHex>;
const toHex27: OmitThisParameterExample = toHex.bind(27);
console.log(toHex27()); // 1b
/**
* OmitThisParameterExample
* () => string
*/2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ThisType
ThisType<Type>可以在对象字面量中键入this,并提供通过上下文类型控制this类型的便捷方式,其只有在--noImplicitThis的选项下才有效。
/**
* Marker for contextual 'this' type
*/
interface ThisType<T> { }
// const foo1 = {
// bar() {
// console.log(this.a); // error
// }
// }
const foo2: { bar: () => void } & ThisType<{ a: number }> = {
bar() {
console.log(this.a); // ok
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Uppercase
Uppercase<StringType>将StringType转为大写,TS以内置关键字intrinsic来通过编译期来实现。
/**
* Convert string literal type to uppercase
*/
type Uppercase<S extends string> = intrinsic;
type UppercaseExample = Uppercase<"abc">;
/**
* UppercaseExample
* ABC
*/2
3
4
5
6
7
8
9
10
11
12
13
14
Lowercase
Lowercase<StringType>将StringType转为小写。
/**
* Convert string literal type to lowercase
*/
type Lowercase<S extends string> = intrinsic;
type LowercaseExample = Lowercase<"ABC">;
/**
* LowercaseExample
* abc
*/2
3
4
5
6
7
8
9
10
11
12
13
14
Capitalize
Capitalize<StringType>将StringType首字母转为大写。
/**
* Convert first character of string literal type to uppercase
*/
type Capitalize<S extends string> = intrinsic;
type CapitalizeExample = Capitalize<"abc">;
/**
* CapitalizeExample
* Abc
*/2
3
4
5
6
7
8
9
10
11
12
13
14
Uncapitalize
Uncapitalize<StringType>将StringType首字母转为小写。
/**
* Convert first character of string literal type to lowercase
*/
type Uncapitalize<S extends string> = intrinsic;
type UncapitalizeExample = Uncapitalize<"ABC">;
/**
* CapitalizeExample
* aBC
*/2
3
4
5
6
7
8
9
10
11
12
13
14
