我有两种类型,它们永远不应该相交。是否有一种方法可以使类型检查器在它们相交时标记出来?理想情况下,我希望在类型世界中纯粹地实现这一点,而不声明任何多余的变量。
示例:
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
const result: Extract<X,Y> = null as never
而不需要中间类型。 - SquattingSlavInTracksuit