-
Notifications
You must be signed in to change notification settings - Fork 0
/
949-anyof.ts
46 lines (37 loc) · 1.7 KB
/
949-anyof.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
949 - AnyOf
-------
by null (@kynefuk) #medium #array
### Question
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:
```ts
type Sample1 = AnyOf<[1, '', false, [], {}]> // expected to be true.
type Sample2 = AnyOf<[0, '', false, [], {}]> // expected to be false.
```
> View on GitHub: https://tsch.js.org/949
*/
/* _____________ Your Code Here _____________ */
type Falsy = 0 | '' | false | [] | {[key: PropertyKey]: never} | null | undefined;
type CheckFalsy<T> = T extends Falsy ? true : false;
type AnyOf<T extends readonly any[]> = CheckFalsy<T[number]> extends true ? false : true;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<AnyOf<[1, 'test', true, [1], { name: 'test' }, { 1: 'test' }]>, true>>,
Expect<Equal<AnyOf<[1, '', false, [], {}]>, true>>,
Expect<Equal<AnyOf<[0, 'test', false, [], {}]>, true>>,
Expect<Equal<AnyOf<[0, '', true, [], {}]>, true>>,
Expect<Equal<AnyOf<[0, '', false, [1], {}]>, true>>,
Expect<Equal<AnyOf<[0, '', false, [], { name: 'test' }]>, true>>,
Expect<Equal<AnyOf<[0, '', false, [], { 1: 'test' }]>, true>>,
Expect<Equal<AnyOf<[0, '', false, [], { name: 'test' }, { 1: 'test' }]>, true>>,
Expect<Equal<AnyOf<[0, '', false, [], {}, undefined, null]>, false>>,
Expect<Equal<AnyOf<[]>, false>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/949/answer
> View solutions: https://tsch.js.org/949/solutions
> More Challenges: https://tsch.js.org
*/