Skip to content

Commit 4c42d89

Browse files
Add OrAll type (#1378)
Co-authored-by: Som Shekhar Mukherjee <iamssmkhrj@gmail.com>
1 parent 878b6df commit 4c42d89

File tree

5 files changed

+142
-13
lines changed

5 files changed

+142
-13
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export type {IfNull} from './source/if-null.d.ts';
156156
export type {IsUndefined} from './source/is-undefined.d.ts';
157157
export type {And} from './source/and.d.ts';
158158
export type {Or} from './source/or.d.ts';
159+
export type {OrAll} from './source/or-all.d.ts';
159160
export type {Xor} from './source/xor.d.ts';
160161
export type {AllExtend, AllExtendOptions} from './source/all-extend.d.ts';
161162
export type {SomeExtend, SomeExtendOptions} from './source/some-extend.d.ts';

readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@ Click the type names for complete docs.
180180
- [`AllUnionFields`](source/all-union-fields.d.ts) - Create a type with all fields from a union of object types.
181181
- [`DistributedOmit`](source/distributed-omit.d.ts) - Omits keys from a type, distributing the operation over a union.
182182
- [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
183-
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
184-
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is true.
185-
- [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is true.
183+
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both `true`.
184+
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is `true`.
185+
- [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is `true`.
186+
- [`OrAll`](source/or-all.d.ts) - Returns a boolean for whether any of the given elements is `true`.
186187
- [`AllExtend`](source/all-extend.d.ts) - Returns a boolean for whether every element in an array type extends another type.
187188
- [`SomeExtend`](source/some-extend.d.ts) - Returns a boolean for whether some element in an array type extends another type.
188189
- [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.

source/or-all.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type {SomeExtend} from './some-extend.d.ts';
2+
3+
/**
4+
Returns a boolean for whether any of the given elements is `true`.
5+
6+
Use-cases:
7+
- Check if at least one condition in a list of booleans is met.
8+
9+
@example
10+
```
11+
import type {OrAll} from 'type-fest';
12+
13+
type FFT = OrAll<[false, false, true]>;
14+
//=> true
15+
16+
type FFF = OrAll<[false, false, false]>;
17+
//=> false
18+
```
19+
20+
Note: When `boolean` is passed as an element, it is distributed into separate cases, and the final result is a union of those cases.
21+
For example, `OrAll<[false, boolean]>` expands to `OrAll<[false, true]> | OrAll<[false, false]>`, which simplifies to `true | false` (i.e., `boolean`).
22+
23+
@example
24+
```
25+
import type {OrAll} from 'type-fest';
26+
27+
type A = OrAll<[false, boolean]>;
28+
//=> boolean
29+
30+
type B = OrAll<[true, boolean]>;
31+
//=> true
32+
```
33+
34+
Note: If `never` is passed as an element, it is treated as `false` and the result is computed accordingly.
35+
36+
@example
37+
```
38+
import type {OrAll} from 'type-fest';
39+
40+
type A = OrAll<[never, never, true]>;
41+
//=> true
42+
43+
type B = OrAll<[never, never, false]>;
44+
//=> false
45+
46+
type C = OrAll<[never, never, never]>;
47+
//=> false
48+
49+
type D = OrAll<[never, never, boolean]>;
50+
//=> boolean
51+
```
52+
53+
Note: If `any` is passed as an element, it is treated as `boolean` and the result is computed accordingly.
54+
55+
@example
56+
```
57+
import type {OrAll} from 'type-fest';
58+
59+
type A = OrAll<[false, any]>;
60+
//=> boolean
61+
62+
type B = OrAll<[true, any]>;
63+
//=> true
64+
```
65+
66+
Note: `OrAll<[]>` evaluates to `false` because there are no `true` elements in an empty tuple. See [Wikipedia: Clause (logic) > Empty clauses](https://en.wikipedia.org/wiki/Clause_(logic)#Empty_clauses:~:text=The%20truth%20evaluation%20of%20an%20empty%20disjunctive%20clause%20is%20always%20false.).
67+
68+
@see {@link Or}
69+
*/
70+
export type OrAll<T extends readonly boolean[]> = SomeExtend<T, true>;
71+
72+
export {};

source/or.d.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type {If} from './if.d.ts';
2-
import type {IsNever} from './is-never.d.ts';
1+
import type {OrAll} from './or-all.d.ts';
32

43
/**
54
Returns a boolean for whether either of two given types is true.
@@ -74,16 +73,10 @@ type G = Or<never, never>;
7473
//=> false
7574
```
7675
76+
@see {@link OrAll}
7777
@see {@link And}
7878
@see {@link Xor}
7979
*/
80-
export type Or<A extends boolean, B extends boolean> =
81-
_Or<If<IsNever<A>, false, A>, If<IsNever<B>, false, B>>; // `never` is treated as `false`
82-
83-
export type _Or<A extends boolean, B extends boolean> = A extends true
84-
? true
85-
: B extends true
86-
? true
87-
: false;
80+
export type Or<A extends boolean, B extends boolean> = OrAll<[A, B]>;
8881

8982
export {};

test-d/or-all.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {expectType} from 'tsd';
2+
import type {OrAll} from '../source/or-all.d.ts';
3+
4+
declare const boolean: boolean;
5+
6+
// Basic boolean combinations
7+
expectType<OrAll<[true, false]>>(true);
8+
expectType<OrAll<[false, true]>>(true);
9+
expectType<OrAll<[true, true]>>(true);
10+
expectType<OrAll<[false, false]>>(false);
11+
12+
// Multiple elements in a tuple
13+
expectType<OrAll<[false, false, false, true]>>(true);
14+
expectType<OrAll<[false, false, false, false]>>(false);
15+
16+
// `boolean` element
17+
expectType<OrAll<[false, false, boolean]>>(boolean);
18+
expectType<OrAll<[false, boolean, true]>>(true);
19+
expectType<OrAll<[boolean, boolean, boolean]>>(boolean);
20+
21+
// Unions
22+
expectType<OrAll<[false, false, true] | [false, false, false]>>(boolean); // `true` | `false`
23+
expectType<OrAll<[false, true, false] | [true]>>(true); // `true` | `true`
24+
expectType<OrAll<[false] | [false, false, false]>>(false); // `false` | `false`
25+
expectType<OrAll<[true, false] | [false, boolean]>>(boolean); // `true` | `boolean`
26+
expectType<OrAll<[false, false] | [false, false, boolean]>>(boolean); // `false` | `boolean`
27+
expectType<OrAll<[boolean, false, false] | [boolean]>>(boolean); // `boolean` | `boolean`
28+
29+
// Tuples with rest element
30+
expectType<OrAll<[false, ...Array<false>]>>(false);
31+
expectType<OrAll<[...Array<false>, true]>>(true);
32+
expectType<OrAll<[false, ...Array<false>, boolean]>>(boolean);
33+
34+
// Non-tuple arrays
35+
expectType<OrAll<Array<true>>>(true);
36+
expectType<OrAll<Array<false>>>(false);
37+
expectType<OrAll<boolean[]>>(boolean);
38+
39+
// Readonly arrays
40+
expectType<OrAll<readonly [true, false, true]>>(true);
41+
expectType<OrAll<readonly [false, false, false]>>(false);
42+
expectType<OrAll<readonly [false, false, boolean]>>(boolean);
43+
expectType<OrAll<ReadonlyArray<true>>>(true);
44+
expectType<OrAll<ReadonlyArray<false>>>(false);
45+
expectType<OrAll<readonly boolean[]>>(boolean);
46+
47+
// Boundary cases
48+
expectType<OrAll<[]>>(false);
49+
50+
expectType<OrAll<[any, any, false]>>(boolean);
51+
expectType<OrAll<[any, any, true]>>(true);
52+
expectType<OrAll<[any, any, any]>>(boolean);
53+
54+
expectType<OrAll<[never, never, false]>>(false);
55+
expectType<OrAll<[never, never, true]>>(true);
56+
expectType<OrAll<[never, never, never]>>(false);
57+
58+
// Errors with non-boolean or optional elements
59+
// @ts-expect-error
60+
type Error1 = OrAll<[1, 0]>;
61+
// @ts-expect-error
62+
type Error2 = OrAll<[true, false?]>;

0 commit comments

Comments
 (0)