首页 > 其他分享 >[Typescript] 39. Medium - AnyOf

[Typescript] 39. Medium - AnyOf

时间:2022-09-28 01:44:06浏览次数:54  
标签:39 Typescript false type extends Array true AnyOf

Implement Python liked any function in the type system. A type takes the Array and returns true if any element of the Array is true. If the Array is empty, return false.

For example:

type Sample1 = AnyOf<[1, '', false, [], {}]> // expected to be true.
type Sample2 = AnyOf<[0, '', false, [], {}]> // expected to be false.

 

type Falsy = false | 0 | '' | [] | Record<PropertyKey, never>
type AnyOf<T extends readonly any[]> = T extends [infer First, ...(infer REST)]
  ? First extends Falsy 
    ? AnyOf<REST>
    : true
  : false;

 

type AnyOf<T extends readonly any[]> = T extends Array<0 | '' | false | [] | Record<PropertyKey, never>> ? false : true

 

标签:39,Typescript,false,type,extends,Array,true,AnyOf
From: https://www.cnblogs.com/Answer1215/p/16736592.html

相关文章