Skip to content

Commit af4bebc

Browse files
authored
ReadonlyTuple: Deprecate in favor of TupleOf (#1256)
1 parent 785549f commit af4bebc

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ Click the type names for complete docs.
345345
- `HomomorphicOmit` - See [`Except`](source/except.d.ts)
346346
- `IfAny`, `IfNever`, `If*` - See [`If`](source/if.d.ts)
347347
- `MaybePromise` - See [`Promisable`](source/promisable.d.ts)
348+
- `ReadonlyTuple` - See [`TupleOf`](source/tuple-of.d.ts)
348349

349350
## Tips
350351

source/readonly-tuple.d.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
/**
2-
Creates a read-only tuple of type `Element` and with the length of `Length`.
3-
4-
@private
5-
@see `ReadonlyTuple` which is safer because it tests if `Length` is a specific finite number.
6-
*/
7-
type BuildTupleHelper<Element, Length extends number, Rest extends Element[]> =
8-
Rest['length'] extends Length ?
9-
readonly [...Rest] : // Terminate with readonly array (aka tuple)
10-
BuildTupleHelper<Element, Length, [Element, ...Rest]>;
1+
import type {TupleOf} from './tuple-of.d.ts';
112

123
/**
134
Create a type that represents a read-only tuple of the given type and length.
@@ -32,12 +23,10 @@ guestFencingTeam.push('Sam');
3223
//=> error TS2339: Property 'push' does not exist on type 'FencingTeam'
3324
```
3425
26+
@deprecated This type will be removed in the next major version. Use the built-in `Readonly` type in combination with the {@link TupleOf} type instead, like `Readonly<TupleOf<Length, Element>>`.
27+
3528
@category Utilities
3629
*/
37-
export type ReadonlyTuple<Element, Length extends number> =
38-
number extends Length
39-
// Because `Length extends number` and `number extends Length`, then `Length` is not a specific finite number.
40-
? readonly Element[] // It's not fixed length.
41-
: BuildTupleHelper<Element, Length, []>; // Otherwise it is a fixed length tuple.
30+
export type ReadonlyTuple<Element, Length extends number> = Readonly<TupleOf<Length, Element>>;
4231

4332
export {};

source/tuple-of.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type EmptyTuple = TupleOf<-3, string>;
6161
//=> []
6262
```
6363
64+
Note: If you need a readonly tuple, simply wrap this type with `Readonly`, for example, to create `readonly [number, number, number]` use `Readonly<TupleOf<3, number>>`.
65+
6466
@category Array
6567
*/
6668
export type TupleOf<Length extends number, Fill = unknown> = IfNotAnyOrNever<Length,

0 commit comments

Comments
 (0)