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
Add MultidimensionalArray
type
#277
Conversation
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 |
Do we really need a separate type for readonly array? In our other types we just handle it. For example: 8f70e88 |
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. |
Nah. I think it's better with two separate types than a boolean argument. |
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.