示例工程 16-js-to-ts-migration
16-js-to-ts-migration/04-strict-migration/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;
import { sum } from './src/main.js';
type Case1 = Expect<Equal<Parameters<typeof sum>, [values: readonly number[]]>>;
type Case2 = Expect<Equal<ReturnType<typeof sum>, number>>;
// 只读数组不能接收可变数组的写入操作
// @ts-expect-error: readonly 数组没有 push
const bad = (values: readonly number[]) => values.push(1);