在ts-toolbelt的源代码中,经常存在空对象和映射类型的交集。这样做的目的是什么?
export type ReadonlyFlat<O> = {
+readonly [K in keyof O]: O[K];
} & {}; // there
export type OptionalFlat<O> = {
[K in keyof O]?: O[K];
} & {}; // there
type __Pick<O extends object, K extends keyof O> = {
[P in K]: O[P];
} & {}; // there
Run Code Online (Sandbox Code Playgroud) 我正在研究 ts-toolbelt 库的源代码。而我也经常遇到这样的表情O extends unknown。在我看来,它没有添加任何功能。
所以我想知道,它是做什么用的?
/**
* @hidden
*/
export type _UnionOf<O extends object> =
O[keyof O]
/**
* Transform an [[Object]] into an [[Union]]
* @param O to transform
* @returns [[Any]]
* @example
* ```ts
* ```
*/
export type UnionOf<O extends object> =
O extends unknown
? _UnionOf<O>
: never
Run Code Online (Sandbox Code Playgroud)
由于某种原因,不是导出_UnionOf类型,而是在其前面加上表达式O extends unknown