Realizable generic tools
Pick
definition
From T, pick a set of properties whose keys are in the union K
The T
: pick out some attribute sets K
to form a new type, for example:
type Parent = {
name: string;
age: number;
}
// 现在 Child 就有了 age 这个属性
type Child = Pick<Parent, 'age'>
Source code
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
Exclude
definition
Exclude from T those types that are assignable to U
T
: some attributes are removed from 060bc72825c4fe, for example:
type GirlFriend = 'girl friend'
type Parent = {
name: string;
age: number;
girlFriend: GirlFriend;
}
type Child = Exclude<keyof Parent, 'girlFriend'>
⚠Note that Child
equivalent to type Child = 'name' | 'age'
Source code
type Exclude<T, U> = T extends U ? never : T;
Omit
definition
Construct a type with the properties of T except for those in type K.
Pick
and Exclude
actually very basic tool generics. In many cases, if you directly use Pick
or Exclude
effect is not as direct as writing directly. And Omit
is a more abstract encapsulation based on these two foundations, which allows you to remove several attributes from an object, and the rest is the new type you need. Below is an example:
type GirlFriend = 'girl friend'
type Parent = {
name: string;
age: number;
girlFriend: GirlFriend;
}
type Child = Omit<Parent, 'girlFriend'>
// 等价于
// type Child = {
// name: string;
// age: number;
// }
Source code
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
Partial
definition
Make all properties in T optional
T
: make all the attributes of 060bc72825c62d become optional attributes, for example 👇
type Root = {
name: string;
age: number;
}
type Test = Partial<Root>
// 等价于
// type Test = {
// name?: string;
// age?: number;
// }
In this way, all attributes in the new type will become optional attributes (optional
Source code
type Partial<T> = {
[P in keyof T]?: T[P];
};
Required
definition
Make all properties in T required
Partial
: just the opposite of Required
, 060bc72825c6b1 will make all attributes become mandatory.
Source code
type Required<T> = {
[P in keyof T]-?: T[P];
};
ReturnType
definition
Obtain the return type of a function type
To the effect: return the type of the value passed in, if it is a function, return the type of its return value.
How to use it? For example, I want to get the function foo
return type 👇
function foo(type): boolean {
return type === 0
}
Then I can use ReturnType
to create one for foo
type, such 👇
type FooType = ReturnType<typeof foo>
⚠️Note: The reason why typeof
is used here is to obtain foo
, which is equivalent to (type: any) => boolean
Source code
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : T
Built-in generic tools
Uppercase
definition
Convert string literal type to uppercase
Main idea: convert all characters to uppercase, for example, I can use it like this 👇
type Test = 'a' | 'b' | 'c'
type UpTest = Uppercase<Test>
// 等价于
// type UpTest = Uppercase<'A' | 'B' | 'C'>
Source code
type Uppercase<S extends string> = intrinsic;
Lowercase
definition
Convert string literal type to lowercase*
The main idea: convert all characters to lowercase, for example 👇
type Test = 'A' | 'B' | 'C'
type LowTest = Lowercase<Test>
// 等价于 type Lowtest = 'a' | 'b' | 'c'
Source code
type Lowercase<S extends string> = intrinsic;
Capitalize
definition
Convert first character of string literal type to uppercase
Main idea: Convert the first character of the string to uppercase, for example 👇
type Test = 'apple'
type CapTest = Capitalize<Test>
// 等价于 type CapTest = 'Apple'
Source code
type Capitalize<S extends string> = intrinsic;
Uncapitalize
definition
Convert first character of string literal type to lowercase
Main idea: Convert the first character of the string to lowercase, for example 👇
type Test = 'Apple'
type CapTest = Uncapitalize<Test>
// 等价于 type CapTest = 'apple'
Source code
type Uncapitalize<S extends string> = intrinsic;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。