【发布时间】:2020-10-21 22:54:11
【问题描述】:
我有 2 种不应该相交的类型。有没有办法在他们这样做时制作类型检查器标志?理想情况下,我想纯粹在类型世界中执行此操作,而不声明任何冗余变量。
示例:
type A = 1 | 2 // Must be different from B
type B_OK = 3
type B_FAIL = 2 | 3
// What I want (pseudo Typescript)
type AssertDifferent<X,Y> = Extract<X,Y> extends never ? any : fail // Fail if the types intersect
// Expected result (pseudo Typescript)
AssertDifferent<A,B_OK> // TS is happy
AssertDifferent<A,B_FAIL> // Fails type check
【问题讨论】:
标签: typescript types typescript-typings