Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultidimensionalArray type #277

Merged
merged 7 commits into from Oct 12, 2021

Conversation

Menecats
Copy link
Contributor

@Menecats Menecats commented Oct 4, 2021

MultidimensionalArray type allows you to type multidimensional array declaratively.

This is useful when a funciton returns a multidimentional array were it's dimensions depends on the input of the functions.
e.g.

function createNDimensionalGrid<Depth extends number>(depth: Depth): MultidimensionalArray<number, Depth> {
  // [...]
}

const myGrid = createNDimensionalGrid(2);
const myOtherGrid = createNDimensionalGrid(9);

myGrid[0][5] = 3;
myOtherGrid[0][1][2][3][4][5][6][7][8] = 9;

@sindresorhus
Copy link
Owner

Thanks for contributing a type. Make sure you read and follow: https://github.com/sindresorhus/type-fest/blob/main/.github/contributing.md#submitting-a-new-type

source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
source/multidimensional-array.d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Do we really need a separate type for readonly array? In our other types we just handle it. For example: 8f70e88

@Menecats
Copy link
Contributor Author

Menecats commented Oct 7, 2021

I think that having the mutable version of this array is a useful option, in fact in a project that I'm currently working on I need it to be mutable.

If you prefer to have only one type definition instead of two, I've just came up with the following:

export type MultidimensionalArray<Element, Dimensions extends number, Readonly extends boolean = true> = number extends Dimensions
	? Recursive<Element, Readonly>
	: IsEqual<Dimensions, 0> extends true
		? Element
		: true extends Readonly
			? ReadonlyArray<MultidimensionalArray<Element, Subtract<Dimensions, 1>, Readonly>>
			: Array<MultidimensionalArray<Element, Subtract<Dimensions, 1>, Readonly>>;

type Recursive<T, Readonly extends boolean> = true extends Readonly
	? ReadonlyArray<Recursive<T, Readonly>>
	: Array<Recursive<T, Readonly>>;


// An example of how a user can use this type

declare const a: MultidimensionalArray<number, 3>; // Readonly
declare const b: MultidimensionalArray<number, 4, true>; // Readonly
declare const c: MultidimensionalArray<number, 5, false>; // Mutable

type MyMutableMultidimensionalArray<T, D extends number> = MultidimensionalArray<T, D, false>;
declare const d: MyMutableMultidimensionalArray<number, 6>; // Mutable

This will allow a user to specify weather or not it wants the MultidimensionalArray to be Readonly.
If you think it's a good idea I can proceed changing my pull request by removing the MultidimensionalReadonlyArray and updating the MultidimensionalArray with these changes

@sindresorhus
Copy link
Owner

Nah. I think it's better with two separate types than a boolean argument.

@sindresorhus sindresorhus merged commit a726d80 into sindresorhus:main Oct 12, 2021
@Menecats Menecats deleted the multidimensional-array branch October 15, 2021 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants