示例工程 13-declaration-files
13-declaration-files/01-ambient-declaration/types.test-d.ts
点击「运行示例」后,左侧显示源码、右侧显示运行结果;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;
// 全局声明在类型层可见:版本是 string,add 返回 number
type Case1 = Expect<Equal<typeof LegacyMath.version, string>>;
type Case2 = Expect<Equal<ReturnType<typeof LegacyMath.add>, number>>;
// 不能篡改只读版本
// @ts-expect-error: version 是 readonly
LegacyMath.version = '2.0.0';