其他分享
首页 > 其他分享> > Typescript类型体操 - IsNever

Typescript类型体操 - IsNever

作者:互联网

题目

中文

实现一个以 T 作为泛型参数的 IsNever类型.
如果 Tnever, 返回 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;

在线演示

标签:Typescript,false,true,never,IsNever,体操,expected,type
来源: https://www.cnblogs.com/laggage/p/type-challenge-is-never.html