source: trip-planner-front/node_modules/type-fest/source/fixed-length-array.d.ts@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/**
2Methods to exclude.
3*/
4type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift';
5
6/**
7Create 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
9Please participate in [this issue](https://github.com/microsoft/TypeScript/issues/26223) if you want to have a similiar type built into TypeScript.
10
11Use-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```
18import {FixedLengthArray} from 'type-fest';
19
20type FencingTeam = FixedLengthArray<string, 3>;
21
22const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert'];
23
24const homeFencingTeam: FencingTeam = ['George', 'John'];
25//=> error TS2322: Type string[] is not assignable to type 'FencingTeam'
26
27guestFencingTeam.push('Sam');
28//=> error TS2339: Property 'push' does not exist on type 'FencingTeam'
29```
30*/
31export 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};
Note: See TracBrowser for help on using the repository browser.