-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Add ReadonlyTuple
type
#383
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c622f1e
New ReadonlyTuple type
Menecats ed2aa07
Update readonly-tuple.d.ts
sindresorhus 31aa0ec
Update readme.md
sindresorhus 30cd285
Update fixed-length-array.d.ts
sindresorhus 50df32f
Update readonly-tuple.d.ts
sindresorhus 2db7e06
Merge branch 'main' into new_readonly_tuple
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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,40 @@ | ||
/** | ||
* Creates a readonly tuple of <Element> and length 'Length' | ||
* @private | ||
* @see ReadonlyTuple which is safer because it tests if `Length` is a specific finite number | ||
*/ | ||
type BuildTupleHelper<Element, Length extends number, Rest extends Element[]> = | ||
Rest['length'] extends Length ? | ||
readonly [...Rest] : // Terminate with readonly array (aka tuple) | ||
BuildTupleHelper<Element, Length, [Element, ...Rest]>; | ||
|
||
/** | ||
Create a type that represents a tuple of the given type and length. | ||
|
||
Use-cases: | ||
- Declaring fixed-length tuples with a large number of items. | ||
- Creating a range union (for example, `0 | 1 | 2 | 3 | 4` from the keys of such a type) without having to resort to recursive types. | ||
- Creating a tuple of coordinates with a static length, for example, length of 3 for a 3D vector. | ||
|
||
@example | ||
``` | ||
import {ReadonlyTuple} from 'type-fest'; | ||
|
||
type FencingTeam = ReadonlyTuple<string, 3>; | ||
|
||
const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert']; | ||
|
||
const homeFencingTeam: FencingTeam = ['George', 'John']; | ||
//=> error TS2322: Type string[] is not assignable to type 'FencingTeam' | ||
|
||
guestFencingTeam.push('Sam'); | ||
//=> error TS2339: Property 'push' does not exist on type 'FencingTeam' | ||
``` | ||
|
||
@category Utilities | ||
*/ | ||
export type ReadonlyTuple<Element, Length extends number> = | ||
number extends Length | ||
// Because `Length extends number` and `number extends Length`, then Length is not a specific finite number | ||
? readonly Element[] // It's not fixed length | ||
: BuildTupleHelper<Element, Length, []>; // Otherwise it is fixed length tuple |
This file contains 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,16 @@ | ||
import {expectAssignable, expectError, expectNotAssignable} from 'tsd'; | ||
import {ReadonlyTuple} from '../index'; | ||
|
||
type TupleOfThreeStrings = ReadonlyTuple<string, 3>; | ||
|
||
expectAssignable<TupleOfThreeStrings>(['a', 'b', 'c']); | ||
|
||
expectNotAssignable<TupleOfThreeStrings>(['a', 'b', 123]); | ||
expectNotAssignable<TupleOfThreeStrings>(['a']); | ||
expectNotAssignable<TupleOfThreeStrings>(['a', 'b']); | ||
expectNotAssignable<TupleOfThreeStrings>(['a', 'b', 'c', 'd']); | ||
|
||
declare const test: TupleOfThreeStrings; | ||
|
||
expectError(test.push); | ||
expectError(test[2] = 'a'); |
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.
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.
I'm not sure if it makes sense to deprecate this. Someone might want a mutable version. I think we should document how this differs from
ReadonlyTuple
though.