1

Among the TypeScript Challenge problems, there is a problem called IsNever. Implement a type IsNever, which takes input type T. If the type of resolves to never, return true, otherwise false.


    type A = IsNever<never> expected to be true
    type B = IsNever<undefined>  expected to be false
    type C = IsNever<null>  expected to be false
    type D = IsNever<[]>  expected to be false
    type E = IsNever<number>  expected to be false
    

my solution(wrong answer)

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

another solution

type IsNever<T> = [T] extends [never] ? true: false;

What's the difference between these two solutions? I wonder what's different when comparing types with tuples.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
DAEWON
  • 11
  • 1

1 Answers1

2

This is how distributive conditional types work.

Think about types as sets of values. Then T is a set of all values assignable to T, and never is an empty set. The first syntax basically says (loosely speaking) "does the set T contains empty set?". Of course it does. Every set, even an empty one, contains an empty set.

Parzh from Ukraine
  • 7,999
  • 3
  • 34
  • 65