19 lines
912 B
TypeScript
19 lines
912 B
TypeScript
|
||
/**
|
||
* 联合类型转换为交叉类型
|
||
* 联合类型是 A | B,结果就是 A & B
|
||
* type Result = UnionToIntersection<string | number> => string & number
|
||
* type Result2 = UnionToIntersection<{ a: string } | { b: number }> => { a: string } & { b: number }
|
||
*/
|
||
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
||
|
||
export type Primitive = string | number | symbol | bigint | boolean | null | undefined;
|
||
/**
|
||
* 字面量联合类型
|
||
* type Status = LiteralUnion<'active' | 'inactive', string> => :'active' | 'inactive' | string
|
||
* type Score = LiteralUnion<0 | 1 | 2 | 3, number> => 0 | 1 | 2 | 3 | number
|
||
*/
|
||
export type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>);
|
||
|
||
export type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
|