ghaiklor / type-challenges-solutions

Solutions for the collection of TypeScript type challenges with explanations
https://ghaiklor.github.io/type-challenges-solutions/
Creative Commons Attribution 4.0 International
470 stars 56 forks source link

type-challenges-solutions/en/medium-isnever #100

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

IsNever

This project is aimed at helping you better understand how the type system works, writing your own utilities, or just having fun with the challenges.

https://ghaiklor.github.io/type-challenges-solutions/en/medium-isnever.html

likui628 commented 2 years ago

[T] extends [never] stop it being distributive ➡Conditional Types - Checking extends never only works sometimes

ghaiklor commented 2 years ago

@likui628 you are right. When I was solving the challenge I thought that is why it works. But later on I've figured out that using [] syntax dismisses the distributivity on type.

dgh500 commented 1 year ago

This should work with anything in place of 'foo' ( except never itself ) as never 'cancels out' of a union where any other type does not

type IsNever<T> = T | 'foo' extends 'foo' ? true : false