示例工程 02-basic-types-inference

02-basic-types-inference/05-satisfies/types.test-d.ts

typescript · 0.6 KB

运行:02 · satisfies

点击「运行示例」后,左侧显示源码、右侧显示运行结果;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 type { routes, routesAnnotated } from './src/main.js';
 
// satisfies 保留了每个成员的具体字面量
type Case1 = Expect<Equal<(typeof routes)['home']['method'], 'GET'>>;
type Case2 = Expect<Equal<(typeof routes)['save']['method'], 'POST'>>;
// 标注写法只剩联合类型,并且 noUncheckedIndexedAccess 让索引访问多出 undefined
type Case3 = Expect<
  Equal<NonNullable<(typeof routesAnnotated)['home']>['method'], 'GET' | 'POST'>
>;