I am working on typescript-challenge 399. Tuple Filter (https://github.com/type-challenges/type-challenges/blob/main/questions/00399-hard-tuple-filter/README.md)
This is my code
type FilterOut<T extends any[], F, Res extends unknown[] = []> =
T extends [infer X, ...infer Y]
? X extends F
? FilterOut<Y, F, Res>
: FilterOut<Y, F, [...Res, X]>
: Res
type t0 = FilterOut<['a', 'b'],'a'> // ['b'] -> Just as I expected
type t1 = FilterOut<[never], never> // never -> But I expect []
type t2 = FilterOut<[never, 'a'], never> // never -> But I expect ['a']
As you can see, t1 and t2 are different from my expectation. I know if I replace X extends F
to [X] extends [F]
, then I can fix this question.
However, I just wonder why we will obtain never
in the first version code? Is this due to some weird properties of never
?
Here I try another thing about never
, and I think it maybe due to the same reason from the previous question.
type t3 = never extends never ? 1 : 2 // 1 -> As I expected
type test<T, F> =
T extends F
? 1
: 2
type t4 = test<never, never> // never -> But I expect 1
Can anybody help me or give me some documentation about this question! Thanks a lot!