This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for TypeScript puzzle on BFE.dev
9. implement NonNullable<T>

Here's the solution for implementing MyNonNullable<T>:

type MyNonNullable<T> = T extends null | undefined ? never : T;

Explanation:

  • We define MyNonNullable<T> as a conditional type.
  • If T is assignable to null or undefined, MyNonNullable<T> will evaluate to never. This will exclude null and undefined from the resulting type.
  • Otherwise, MyNonNullable<T> will evaluate to T.

That's it! Now we can use MyNonNullable<T> as follows:

type Foo = 'a' | 'b' | null | undefined

type A = MyNonNullable<Foo> // 'a' | 'b'

The resulting type A will exclude null and undefined from Foo.