示例工程 09-conditional-types-infer

09-conditional-types-infer/01-is-string/types.test-d.ts

typescript · 0.5 KB

运行:09 · is-string

点击「运行示例」后,左侧显示源码、右侧显示运行结果;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 { If, IsString } from './src/main.js';
 
type Case1 = Expect<Equal<IsString<'a'>, true>>;
type Case2 = Expect<Equal<IsString<number>, false>>;
 
// 裸类型参数遇到联合会分配:true | false = boolean
type Case3 = Expect<Equal<IsString<string | number>, boolean>>;
 
type Case4 = Expect<Equal<If<true, 'yes', 'no'>, 'yes'>>;
type Case5 = Expect<Equal<If<false, 'yes', 'no'>, 'no'>>;