Sha256: 9ce0473b0fbaf7287afb01b6a91bd38f73a31093e59ee86de1fd3f352f3fc817

Contents?: true

Size: 1.31 KB

Versions: 28

Compression:

Stored size: 1.31 KB

Contents

import type {Subtract} from './subtract';
import type {IsEqual} from './is-equal';

type Recursive<T> = Array<Recursive<T>>;

/**
Creates a type that represents a multidimensional array of the given type and dimension.

Use-cases:
- Return a n-dimensional array from functions.
- Declare a n-dimensional array by defining its dimensions rather than declaring `[]` repetitively.
- Infer the dimensions of a n-dimensional array automatically from function arguments.
- Avoid the need to know in advance the dimensions of a n-dimensional array allowing them to be dynamic.

@example
```
import type {MultidimensionalArray} from 'type-fest';

function emptyMatrix<T extends number>(dimensions: T): MultidimensionalArray<unknown, T> {
	const matrix: unknown[] = [];

	let subMatrix = matrix;
	for (let dimension = 1; dimension < dimensions; ++dimension) {
		console.log(`Initializing dimension #${dimension}`);

		subMatrix[0] = [];
		subMatrix = subMatrix[0] as unknown[];
	}

	return matrix as MultidimensionalArray<unknown, T>;
}

const matrix = emptyMatrix(3);

matrix[0][0][0] = 42;
```

@category Array
*/
export type MultidimensionalArray<Element, Dimensions extends number> = number extends Dimensions
	? Recursive<Element>
	: IsEqual<Dimensions, 0> extends true
		? Element
		: Array<MultidimensionalArray<Element, Subtract<Dimensions, 1>>>;

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/type-fest/source/multidimensional-array.d.ts