Skip to content

Commit d08f010

Browse files
kainiedzielasindresorhus
andauthoredMay 21, 2020
Add FixedLengthArray type (#106)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 840fba9 commit d08f010

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
 

‎index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {ConditionalKeys} from './source/conditional-keys';
2323
export {ConditionalPick} from './source/conditional-pick';
2424
export {UnionToIntersection} from './source/union-to-intersection';
2525
export {Stringified} from './source/stringified';
26+
export {FixedLengthArray} from './source/fixed-length-array';
2627

2728
// Miscellaneous
2829
export {PackageJson} from './source/package-json';

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Click the type names for complete docs.
8282
- [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
8383
- [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
8484
- [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
85+
- [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
8586

8687
### Miscellaneous
8788

‎source/fixed-length-array.d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
Methods to exclude.
3+
*/
4+
type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift';
5+
6+
/**
7+
Create a type that represents an array of the given type and length. The array's length and the `Array` prototype methods that manipulate its length are excluded in the resulting type.
8+
9+
Please participate in [this issue](https://github.com/microsoft/TypeScript/issues/26223) if you want to have a similiar type built into TypeScript.
10+
11+
Use-cases:
12+
- Declaring fixed-length tuples or arrays with a large number of items.
13+
- 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.
14+
- Creating an array of coordinates with a static length, for example, length of 3 for a 3D vector.
15+
16+
@example
17+
```
18+
import {FixedLengthArray} from 'type-fest';
19+
20+
type FencingTeam = FixedLengthArray<string, 3>;
21+
22+
const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert'];
23+
24+
const homeFencingTeam: FencingTeam = ['George', 'John'];
25+
//=> error TS2322: Type string[] is not assignable to type 'FencingTeam'
26+
27+
guestFencingTeam.push('Sam');
28+
//=> error TS2339: Property 'push' does not exist on type 'FencingTeam'
29+
```
30+
*/
31+
export type FixedLengthArray<Element, Length extends number, ArrayPrototype = [Element, ...Element[]]> = Pick<
32+
ArrayPrototype,
33+
Exclude<keyof ArrayPrototype, ArrayLengthMutationKeys>
34+
> & {
35+
[index: number]: Element;
36+
[Symbol.iterator]: () => IterableIterator<Element>;
37+
readonly length: Length;
38+
};

‎test-d/fixed-length-array.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {expectAssignable, expectNotAssignable} from 'tsd';
2+
import {FixedLengthArray} from '..';
3+
4+
type FixedToThreeStrings = FixedLengthArray<string, 3>;
5+
6+
expectAssignable<FixedToThreeStrings>(['a', 'b', 'c']);
7+
8+
expectNotAssignable<FixedToThreeStrings>(['a', 'b', 123]);
9+
expectNotAssignable<FixedToThreeStrings>(['a']);
10+
expectNotAssignable<FixedToThreeStrings>(['a', 'b']);
11+
expectNotAssignable<FixedToThreeStrings>(['a', 'b', 'c', 'd']);

0 commit comments

Comments
 (0)
Please sign in to comment.