【发布时间】:2019-08-23 04:56:18
【问题描述】:
这个很直接,这里是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]>>;
【问题讨论】:
-
不是
Extract<T[number], false>-never?所以不是Not<Not<IsNever<Extract<T[number], false>>>>-true? -
@zerkms 并非总是如此,这就是我使用
IsNever<>检查它的原因 -
@zerkms Here you are
-
@Veetaha 这可能是一个错误,一个小得多的复制品:typescriptlang.org/play/…
-
我觉得我也遇到过这样的错误,通常只是通过将中间结果放入默认类型参数来解决它们(例如,
And<T, N=Not<T>> = Not<N>)
标签: typescript generics typescript-generics conditional-types