題目 中文 實現一個以 T 作為泛型參數的 IsNever類型. 如果 T 是never, 返回 true, 否則返回 false. 示例: type A = IsNever<never>; // expected to be true type B = IsNever<undefined>; // ...
題目
中文
實現一個以 T
作為泛型參數的 IsNever
類型.
如果 T
是never
, 返回 true
, 否則返回 false
.
示例:
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
English
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
答案
type IsNever<T extends unknown> = [T] extends [never] ? true : false;
作者:Laggage
出處:https://www.cnblogs.com/laggage/p/type-challenge-is-never.html
說明:轉載請註明來源