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

AI solution for TypeScript puzzle on BFE.dev
7. implement Exclude<T, E>

Here is a possible implementation:

type MyExclude<T, K> = T extends K ? never : T;

Explanation:

  • We use the conditional type syntax T extends K ? X : Y to check if each member of T is assignable to K.
  • If it is (i.e., the condition is true), we return the never type, which means this member should be excluded from the resulting union.
  • If it is not (i.e., the condition is false), we return the original member of T, because it should be included in the resulting union.

This logic allows us to filter out any member of T that is assignable to K, and keep the others. Thus, MyExclude<Foo, 'a'> evaluates to 'b' | 'c', MyExclude<Foo, 'c'> evaluates to 'a' | 'b', MyExclude<Foo, 'c' | 'd'> evaluates to 'a' | 'b', and MyExclude<Foo, 'a' | 'b' | 'c'> evaluates to never.