Sha256: 4ce53edb8fb1d2f8b2f6814084b773cdf5846f49bf5a426fbe4029327bda95bf

Contents?: true

Size: 1.54 KB

Versions: 61

Compression:

Stored size: 1.54 KB

Contents

/**
Creates a read-only tuple of type `Element` and with the length of `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 read-only 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 a fixed length tuple.

Version data entries

61 entries across 61 versions & 3 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.59 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.58 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.57 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.56 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.55 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.54 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.53 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.52 node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.51 node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.26 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
immosquare-cleaner-0.1.50 node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/type-fest/source/readonly-tuple.d.ts