示例工程 13-declaration-files

13-declaration-files/04-declaration-merging/types.test-d.ts

typescript · 0.5 KB

运行:13 · declaration-merging

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

type Equal<A, B> =
  (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type Expect<T extends true> = T;
 
// 两个文件合并出三个键
type Case1 = Expect<Equal<keyof AppEvents, 'ready' | 'error' | 'progress'>>;
type Case2 = Expect<Equal<AppEvents['error'], Error>>;
type Case3 = Expect<Equal<AppEvents['progress'], number>>;
 
// 不存在的键被拒绝
// @ts-expect-error: 'unknown' 不在 AppEvents 中
type Bad = AppEvents['unknown'];