Skip to content
Onsite

IsNever<T> and IsAny<T>

MediumP3Coding

Implement two helper types (named with a My prefix because IsAny already exists in the test helpers).

  • MyIsNever<T>: true if T is exactly never, otherwise false.
  • MyIsAny<T>: true if T is exactly any, otherwise false.

Both are tricky for the same reason: never and any behave differently from other types in conditional types.

Tests your type must pass

type cases = [
  Expect<Equal<MyIsNever<never>, true>>,
  Expect<Equal<MyIsNever<never | string>, false>>,
  Expect<Equal<MyIsNever<''>, false>>,
  Expect<Equal<MyIsNever<undefined>, false>>,
  Expect<Equal<MyIsNever<null>, false>>,
  Expect<Equal<MyIsNever<[]>, false>>,
  Expect<Equal<MyIsNever<{}>, false>>,
  Expect<Equal<MyIsNever<any>, false>>,

  Expect<Equal<MyIsAny<any>, true>>,
  Expect<Equal<MyIsAny<unknown>, false>>,
  Expect<Equal<MyIsAny<never>, false>>,
  Expect<Equal<MyIsAny<string>, false>>,
  Expect<Equal<MyIsAny<undefined>, false>>,
  Expect<Equal<MyIsAny<{}>, false>>,
]

Hints

TypeScript
Run and Submit type-check your solution against the tests.