TypeScript 元组条件比较始终计算为 false

TypeScript tuples conditional comparison always evaluates to false

这个很直接,这里是the link to playground

正如我所注意到的,当您从 If<> 中的条件中删除元组类型时,一切都会按预期进行。但我不想这样做(t2 断言失败),而且我想了解为什么会发生这种情况。这只是一个错误吗?为什么 And<> 类型总是扩展为 true

type If<
    TCond extends boolean,
    TIfTrue,
    TElse
> = [TCond] extends [true] ? TIfTrue : TElse; // if you remove tuples, it works 

type Not<T extends boolean> = If<(T), false, true>;

type IsNever<TSuspect> = TSuspect extends never ? true : false;

type AssertFalse<TSuspect extends false> = TSuspect;
type AssertTrue <TSuspect extends true> = TSuspect;

//   V~~ always true 
type And<T extends boolean[]> = Not<Not<IsNever<Extract<T[number], false>>>>;

type AndExpected<T extends boolean[]> = IsNever<Extract<T[number], false>>;


type t0 = AssertFalse<Not<true>>;  
type t1 = AssertTrue<Not<false>>;  
type t2 = AssertTrue<Not<boolean>>;

type t3 = AssertFalse<And<[false]>>; // ????
type t4 = AssertFalse<AndExpected<[false]>>;

首先,我将尝试解释为什么 If with tuples 与 If without tuples 不同:

type If<
    TCond extends boolean,
    TIfTrue,
    TElse
> = [TCond] extends [true] ? TIfTrue : TElse; // if you remove tuples, it works 

type If2<
    TCond extends boolean,
    TIfTrue,
    TElse
> = TCond extends true ? TIfTrue : TElse;  


type Not<T extends boolean> = If<(T), false, true>;
type Not2<T extends boolean> = If2<(T), false, true>;

// with tuples, Not<boolean> is true because that's how you set it up
type A1 = Not<boolean>; // true
type A2 = Not<true>;    // false
type A3 = Not<false>;   // true

// without typles, TCond extends true is distributive over union types,
// and boolean is really just a union of true | false,
// so Not2<boolean> is boolean 
type B1 = Not2<boolean>; // boolean 
type B2 = Not2<true>;    // false
type B3 = Not2<false>;   // true

好了,现在进入正题。这是我(和 Titian Cernicova-Dragomir)发现的奇怪的东西:

type SameAsT<T extends boolean> = Not<Not<T>>;
type X1 = SameAsT<false>; // true !
type X2 = SameAsT<true>; // true again

// but why?

如果展开所有文字类型,它会按预期工作:

type ReallyNotFalse = [false] extends [true] ? false : true; // true
type ReallyNotNotFalse = [([false] extends [true] ? false : true)] extends [true] ? false : true; // false

看起来像是编译器中的错误。

顺便说一下,Not 基于 If 没有元组的情况下按预期工作

type SameAsT2<T extends boolean> = Not2<Not2<T>>;
type Y1 = SameAsT2<false>; // false
type Y2 = SameAsT2<true>; // true 

因此,可以使用其他方式来抑制条件中的分配联合类型并使相关代码正常工作。一种方法是添加多余的条件,该条件的计算结果始终为真,并且没有 TCond 作为被检查的类型:

type If<
    TCond extends boolean,
    TIfTrue,
    TElse
> = {} extends TCond ? TCond extends true ? TIfTrue : TElse : never; 

type Not<T extends boolean> = If<(T), false, true>;

type IsNever<TSuspect> = TSuspect extends never ? true : false;

type AssertFalse<TSuspect extends false> = TSuspect;
type AssertTrue <TSuspect extends true> = TSuspect;

//   V~~ always true 
type And<T extends boolean[]> = Not<Not<IsNever<Extract<T[number], false>>>>;

type AndExpected<T extends boolean[]> = IsNever<Extract<T[number], false>>;


type t0 = AssertFalse<Not<true>>;  
type t1 = AssertTrue<Not<false>>;  
type t2 = AssertTrue<Not<boolean>>;

type t3 = AssertFalse<And<[false]>>; 
type t4 = AssertFalse<AndExpected<[false]>>;