-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add AndAll type
#1383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add AndAll type
#1383
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f9b692d
feat: add `AndAll` type
som-sm 06452ae
refactor: rewrite `And` with `AndAll`
som-sm 9cb2dc8
test: add more cases
som-sm ace9bfc
doc: add `AndAll` to README
som-sm ab6ec84
doc: improve JSDoc
som-sm 432ba36
doc: link `AndAll` in `And`
som-sm 9a9d377
doc: add note regarding empty tuple (`[]`)
som-sm 45186e3
doc: add missing `@example` tags
som-sm 4533724
test: fix incorrect order
som-sm 960aee2
doc: remove unnecessary `@example` block for empty tuple note
som-sm 8d7aae9
Merge branch 'main' into feat/add-and-all-type
sindresorhus a3552c7
Update readme.md
sindresorhus dae8a69
Update boolean type descriptions in readme
sindresorhus b568b92
doc: add link to `OrAll`/`AndAll`
som-sm 6453c47
doc: improve empty tuple note
som-sm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| 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?]>; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 ([]), returnstrue, unlikeOrAll, which returnsfalse. This behavior is intentional/correct; see:This is the same reason why
Array.prototype.everyreturnstruefor empty arrays, whileArray.prototype.somereturnsfalse.