AnyOf
介绍
在类型系统中实现类似于 Python 中 any
函数。类型接收一个数组,如果数组中任一个元素为真,则返回 true
,否则返回 false
。如果数组为空,返回 false
。
例如:
ts
type Sample1 = AnyOf<[1, '', false, [], {}]> // expected to be true.type Sample2 = AnyOf<[0, '', false, [], {}]> // expected to be false.
View on GitHubts
type Sample1 = AnyOf<[1, '', false, [], {}]> // expected to be true.type Sample2 = AnyOf<[0, '', false, [], {}]> // expected to be false.
起点
ts
/* _____________ Your Code Here _____________ */typeAnyOf <T extends readonly any[]> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[1, 'test', true, [1], {name : 'test' }, { 1: 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[1, '', false, [], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, 'test', false, [], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', true, [], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [1], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], {name : 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], { 1: 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], {name : 'test' }, { 1: 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], {}, undefined, null]>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[]>, false>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeAnyOf <T extends readonly any[]> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[1, 'test', true, [1], {name : 'test' }, { 1: 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[1, '', false, [], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, 'test', false, [], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', true, [], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [1], {}]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], {name : 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], { 1: 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], {name : 'test' }, { 1: 'test' }]>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[0, '', false, [], {}, undefined, null]>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <AnyOf <[]>, false>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeAnyOf <T extends any[]> =T [number] extends 0 | '' | false | [] | {[key : string]: never}? false : true;
ts
// most populartypeAnyOf <T extends any[]> =T [number] extends 0 | '' | false | [] | {[key : string]: never}? false : true;