Basic Utility Types
# 目录
以下方法兼容 Flow (opens new window) 类型。
# $Keys
获取 object 的 keys。
/**
* $Keys
* @desc Get the union type of all the keys in an object type `T`
* @see https://flow.org/en/docs/types/utilities/#toc-keys
* @example
* type Props = { name: string; age: number; visible: boolean };
*
* // Expect: "name" | "age" | "visible"
* type PropsKeys = $Keys<Props>;
*/
export type $Keys<T extends object> = keyof T;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# $Values
获取 object 的 values types。
/**
* $Values
* @desc Get the union type of all the values in an object type `T`
* @see https://flow.org/en/docs/types/utilities/#toc-values
* @example
* type Props = { name: string; age: number; visible: boolean };
*
* // Expect: string | number | boolean
* type PropsValues = $Values<Props>;
*/
export type $Values<T extends object> = T[keyof T];
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# $ReadOnly
获取 deep readonly object type。
/**
* $ReadOnly
* @desc Get the read-only version of a given object type `T` (it works on nested data structure)
* @see https://flow.org/en/docs/types/utilities/#toc-readonly
* @example
* type Props = { name: string; age: number; visible: boolean };
*
* // Expect: Readonly<{ name: string; age: number; visible: boolean; }>
* type ReadOnlyProps = $ReadOnly<Props>;
*/
export type $ReadOnly<T extends object> = DeepReadonly<T>;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- DeepReadonly:object deep readonly type.
# $Diff
获取两个 object 的类型差集。
/**
* $Diff
* @desc Get the set difference of a given object types `T` and `U` (`T \ U`)
* @see https://flow.org/en/docs/types/utilities/#toc-diff
* @example
* type Props = { name: string; age: number; visible: boolean };
* type DefaultProps = { age: number };
*
* // Expect: { name: string; visible: boolean; }
* type RequiredProps = Diff<Props, DefaultProps>;
*/
export type $Diff<T extends U, U extends object> = Pick<
T,
SetComplement<keyof T, keyof U>
>;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- SetComplement:从集合中过滤掉另一个集成中的类型。
# $PropertyType
获取 object 中某个 key 对应的 type。
/**
* $PropertyType
* @desc Get the type of property of an object at a given key `K`
* @see https://flow.org/en/docs/types/utilities/#toc-propertytype
* @example
* // Expect: string;
* type Props = { name: string; age: number; visible: boolean };
* type NameType = $PropertyType<Props, 'name'>;
*
* // Expect: boolean
* type Tuple = [boolean, number];
* type A = $PropertyType<Tuple, '0'>;
* // Expect: number
* type B = $PropertyType<Tuple, '1'>;
*/
export type $PropertyType<T extends object, K extends keyof T> = T[K];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# $ElementType
给定 index 的类型,获取 object、array、tuple 指定下标的类型。
/**
* $ElementType
* @desc Get the type of elements inside of array, tuple or object of type `T`, that matches the given index type `K`
* @see https://flow.org/en/docs/types/utilities/#toc-elementtype
* @example
* // Expect: string;
* type Props = { name: string; age: number; visible: boolean };
* type NameType = $ElementType<Props, 'name'>;
*
* // Expect: boolean
* type Tuple = [boolean, number];
* type A = $ElementType<Tuple, '0'>;
* // Expect: number
* type B = $ElementType<Tuple, '1'>;
*
* // Expect: boolean
* type Arr = boolean[];
* type ItemsType = $ElementType<Arr, number>;
*
* // Expect: number
* type Obj = { [key: string]: number };
* type ValuesType = $ElementType<Obj, string>;
*/
export type $ElementType<
T extends { [P in K & any]: any },
K extends keyof T | number
> = T[K];
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
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
# $Call
获取函数的返回值类型。
/**
* $Call
* @desc Get the return type from a given typeof expression
* @see https://flow.org/en/docs/types/utilities/#toc-call
* @example
* // Common use-case
* const add = (amount: number) => ({ type: 'ADD' as 'ADD', payload: amount });
* type AddAction = $Call<typeof returnOfIncrement>; // { type: 'ADD'; payload: number }
*
* // Examples migrated from Flow docs
* type ExtractPropType<T extends { prop: any }> = (arg: T) => T['prop'];
* type Obj = { prop: number };
* type PropType = $Call<ExtractPropType<Obj>>; // number
*
* type ExtractReturnType<T extends () => any> = (arg: T) => ReturnType<T>;
* type Fn = () => number;
* type FnReturnType = $Call<ExtractReturnType<Fn>>; // number
*/
export type $Call<Fn extends (...args: any[]) => any> = Fn extends (
arg: any
) => infer RT
? RT
: never;
type ReturnType<T> = T extends (...args: any[]) => infer P ? P : any;
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- infer: 表示在 extends 条件语句中待推断的类型变量。
# $Shape
使 object 的所有 property 类型为可选。
/**
* $Shape
* @desc Copies the shape of the type supplied, but marks every field optional.
* @see https://flow.org/en/docs/types/utilities/#toc-shape
* @example
* type Props = { name: string; age: number; visible: boolean };
*
* // Expect: Partial<Props>
* type PartialProps = $Shape<Props>;
*/
export type $Shape<T extends object> = Partial<T>;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# $NonMaybeType
去除 null 、undefined 类型。
/**
* $NonMaybeType
* @desc Excludes null and undefined from T
* @see https://flow.org/en/docs/types/utilities/#toc-nonmaybe
* @example
* type MaybeName = string | null;
*
* // Expect: string
* type Name = $NonMaybeType<MaybeName>;
*/
export type $NonMaybeType<T> = NonNullable<T>;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# Class
获取 class 的类型。
/**
* Class
* @desc Represents constructor of type T
* @see https://flow.org/en/docs/types/utilities/#toc-class
* @example
* class Store {}
* function makeStore(storeClass: Class<Store>): Store {
* return new storeClass();
* }
*/
export type Class<T> = new (...args: any[]) => T;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
上次更新: 2022/09/06, 14:25:16