IsNever
介绍
实现一个类型IsNever, 接收类型T. 如果类型解析为never, 返回true, 否则返回false.
例如:
ts
type A = IsNever<never> // expected to be truetype B = IsNever<undefined> // expected to be falsetype C = IsNever<null> // expected to be falsetype D = IsNever<[]> // expected to be falsetype E = IsNever<number> // expected to be false
View on GitHubts
type A = IsNever<never> // expected to be truetype B = IsNever<undefined> // expected to be falsetype C = IsNever<null> // expected to be falsetype D = IsNever<[]> // expected to be falsetype E = IsNever<number> // expected to be false
起点
ts
/* _____________ Your Code Here _____________ */typeIsNever <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <never>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <never | string>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <''>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <undefined>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <null>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <[]>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <{}>, false>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeIsNever <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <never>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <never | string>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <''>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <undefined>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <null>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <[]>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <IsNever <{}>, false>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeIsNever <T > = [T ] extends [never] ? true : false
ts
// most populartypeIsNever <T > = [T ] extends [never] ? true : false