示例工程 03-literal-types-narrowing

03-literal-types-narrowing/02-typeof-narrowing/types.test-d.ts

typescript · 0.5 KB

运行:03 · typeof-narrowing

点击「运行示例」后,左侧显示源码、右侧显示运行结果;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 { Input, firstValue } from './src/main.js';
 
type Case1 = Expect<Equal<Parameters<typeof firstValue>[0], Input>>;
type Case2 = Expect<Equal<ReturnType<typeof firstValue>, string>>;
 
// 未收窄的联合类型不能直接当 string 用
// @ts-expect-error: string[] 分支上没有 toUpperCase
const bad: string = (undefined as unknown as Input).toUpperCase();
 
export { bad };