-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy patharray-length.d.ts
More file actions
36 lines (25 loc) · 882 Bytes
/
array-length.d.ts
File metadata and controls
36 lines (25 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.
@example
```
import type {ArrayLength} from 'type-fest';
type TupleLength = ArrayLength<[1, 2, 3]>;
//=> 3
type TupleWithOptionalMembersLength = ArrayLength<[1, 2, number?]>;
//=> 2 | 3
type NonTupleArrayLength = ArrayLength<string[]>;
//=> number
type TupleWithRestElementLength = ArrayLength<[1, 2, ...string[]]>;
//=> number
// Distinguish between arrays with fixed and non-fixed lengths
type IsFixedLengthArray<T extends readonly unknown[]> = number extends ArrayLength<T> ? false : true;
type A = IsFixedLengthArray<number[]>;
//=> false
type B = IsFixedLengthArray<[1, 2, 3]>;
//=> true
```
@category Array
*/
export type ArrayLength<T extends readonly unknown[]> = T['length'];
export {};