示例工程 15-type-testing-api-design

15-type-testing-api-design/01-type-assertions/src/main.ts

typescript · 0.5 KB

运行:15 · type-assertions

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

// 演示:类型级断言的最小实现,以及被测试的公开 API
export type Equal<A, B> =
  (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
 
export type Expect<T extends true> = T;
 
export function add(a: number, b: number): number {
  return a + b;
}
 
export function run(log: (msg: string) => void = console.log): string[] {
  const lines = [String(add(1, 2))];
  for (const line of lines) {
    log(line);
  }
  return lines;
}