首页 > 其他分享 >[Typescript] 40. Medium - IsNever

[Typescript] 40. Medium - IsNever

时间:2022-09-28 21:55:31浏览次数:62  
标签:Typescript false true never IsNever Medium expected type

Implement a type IsNever, which takes input type T.
If the type of resolves to never, return true, otherwise false.

For example:

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

Was thinking about doing as such:

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

But sadly it won't work well, it fails at this case: Expect<Equal<IsNever<never>, true>>,

Solution is always:

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

标签:Typescript,false,true,never,IsNever,Medium,expected,type
From: https://www.cnblogs.com/Answer1215/p/16739695.html

相关文章