Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export type {IsNull} from './source/is-null.d.ts';
export type {IfNull} from './source/if-null.d.ts';
export type {IsUndefined} from './source/is-undefined.d.ts';
export type {And} from './source/and.d.ts';
export type {AndAll} from './source/and-all.d.ts';
export type {Or} from './source/or.d.ts';
export type {OrAll} from './source/or-all.d.ts';
export type {Xor} from './source/xor.d.ts';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Click the type names for complete docs.
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both `true`.
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is `true`.
- [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is `true`.
- [`AndAll`](source/and-all.d.ts) - Returns a boolean for whether all of the given elements are `true`.
- [`OrAll`](source/or-all.d.ts) - Returns a boolean for whether any of the given elements is `true`.
- [`AllExtend`](source/all-extend.d.ts) - Returns a boolean for whether every element in an array type extends another type.
- [`SomeExtend`](source/some-extend.d.ts) - Returns a boolean for whether some element in an array type extends another type.
Expand Down
76 changes: 76 additions & 0 deletions source/and-all.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type {AllExtend} from './all-extend.d.ts';

/**
Returns a boolean for whether all of the given elements are `true`.

Use-cases:
- Check if all conditions in a list of booleans are met.

@example
```
import type {AndAll} from 'type-fest';

type TTT = AndAll<[true, true, true]>;
//=> true

type TTF = AndAll<[true, true, false]>;
//=> false

type TFT = AndAll<[true, false, true]>;
//=> false
```

Note: When `boolean` is passed as an element, it is distributed into separate cases, and the final result is a union of those cases.
For example, `AndAll<[true, boolean]>` expands to `AndAll<[true, true]> | AndAll<[true, false]>`, which simplifies to `true | false` (i.e., `boolean`).

@example
```
import type {AndAll} from 'type-fest';

type A = AndAll<[true, boolean]>;
//=> boolean

type B = AndAll<[false, boolean]>;
//=> false
```

Note: If any of the elements is `never`, the result becomes `false`.

@example
```
import type {AndAll} from 'type-fest';

type A = AndAll<[true, true, never]>;
//=> false

type B = AndAll<[false, never, never]>;
//=> false

type C = AndAll<[never, never, never]>;
//=> false

type D = AndAll<[boolean, true, never]>;
//=> false
```

Note: If `any` is passed as an element, it is treated as `boolean` and the result is computed accordingly.

@example
```
import type {AndAll} from 'type-fest';

type A = AndAll<[false, any]>;
//=> false

type B = AndAll<[true, any]>;
//=> boolean
```

Note: `AndAll<[]>` evaluates to `true` due to the concept of [vacuous truth](https://en.wikipedia.org/wiki/Logical_conjunction#:~:text=In%20keeping%20with%20the%20concept%20of%20vacuous%20truth%2C%20when%20conjunction%20is%20defined%20as%20an%20operator%20or%20function%20of%20arbitrary%20arity%2C%20the%20empty%20conjunction%20(AND%2Ding%20over%20an%20empty%20set%20of%20operands)%20is%20often%20defined%20as%20having%20the%20result%20true.), i.e., there are no `false` elements in an empty tuple.

@see {@link And}
@see {@link OrAll}
*/
export type AndAll<T extends readonly boolean[]> = AllExtend<T, true>;

export {};
5 changes: 3 additions & 2 deletions source/and.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {AllExtend} from './all-extend.d.ts';
import type {AndAll} from './and-all.d.ts';

/**
Returns a boolean for whether two given types are both true.
Expand Down Expand Up @@ -73,9 +73,10 @@ type G = And<never, never>;
//=> false
```

@see {@link AndAll}
@see {@link Or}
@see {@link Xor}
*/
export type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
export type And<A extends boolean, B extends boolean> = AndAll<[A, B]>;

export {};
1 change: 1 addition & 0 deletions source/or-all.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type B = OrAll<[true, any]>;
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.).
@see {@link Or}
@see {@link AndAll}
*/
export type OrAll<T extends readonly boolean[]> = SomeExtend<T, true>;

Expand Down
63 changes: 63 additions & 0 deletions test-d/and-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {expectType} from 'tsd';
import type {AndAll} from '../source/and-all.d.ts';

declare const boolean: boolean;

// Basic boolean combinations
expectType<AndAll<[true, true]>>(true);
expectType<AndAll<[true, false]>>(false);
expectType<AndAll<[false, true]>>(false);
expectType<AndAll<[false, false]>>(false);

// Multiple elements in a tuple
expectType<AndAll<[true, true, true, true]>>(true);
expectType<AndAll<[true, true, true, false]>>(false);
expectType<AndAll<[true, true, false, true]>>(false);

// `boolean` element
expectType<AndAll<[true, true, boolean]>>(boolean);
expectType<AndAll<[true, boolean, false]>>(false);
expectType<AndAll<[boolean, boolean, boolean]>>(boolean);

// Unions
expectType<AndAll<[true, true, true] | [true, true, false]>>(boolean); // `true` | `false`
expectType<AndAll<[true, true] | [true]>>(true); // `true` | `true`
expectType<AndAll<[false] | [true, false, true]>>(false); // `false` | `false`
expectType<AndAll<[true, true] | [true, boolean]>>(boolean); // `true` | `boolean`
expectType<AndAll<[false, true] | [true, true, boolean]>>(boolean); // `false` | `boolean`
expectType<AndAll<[boolean, true, true] | [boolean]>>(boolean); // `boolean` | `boolean`

// Tuples with rest element
expectType<AndAll<[true, ...Array<true>]>>(true);
expectType<AndAll<[...Array<true>, false]>>(false);
expectType<AndAll<[true, ...Array<true>, boolean]>>(boolean);

// Non-tuple arrays
expectType<AndAll<Array<true>>>(true);
expectType<AndAll<Array<false>>>(false);
expectType<AndAll<boolean[]>>(boolean);

// Readonly arrays
expectType<AndAll<readonly [true, true, true]>>(true);
expectType<AndAll<readonly [true, true, false]>>(false);
expectType<AndAll<readonly [true, true, boolean]>>(boolean);
expectType<AndAll<ReadonlyArray<true>>>(true);
expectType<AndAll<ReadonlyArray<false>>>(false);
expectType<AndAll<readonly boolean[]>>(boolean);

// Boundary cases
expectType<AndAll<[]>>(true);
Copy link
Copy Markdown
Collaborator Author

@som-sm som-sm Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AndAll, when instantiated with an empty array ([]), returns true, unlike OrAll, which returns false. This behavior is intentional/correct; see:

This is the same reason why Array.prototype.every returns true for empty arrays, while Array.prototype.some returns false.


expectType<AndAll<[any, any, false]>>(false);
expectType<AndAll<[any, any, true]>>(boolean);
expectType<AndAll<[any, any, any]>>(boolean);

expectType<AndAll<[false, never, never]>>(false);
expectType<AndAll<[true, true, never]>>(false);
expectType<AndAll<[never, never, never]>>(false);

// Errors with non-boolean or optional elements
// @ts-expect-error
type Error1 = AndAll<[1, 0]>;
// @ts-expect-error
type Error2 = AndAll<[true, false?]>;
Loading