Open topaxi opened 5 years ago
@topaxi, as an alternative, you may use DeepReadonly<>
type from 'ts-typedefs'
package or use a non-jagged one-dimensional array, that will be more performant:
export class Matrix2D<T> {
private matrix: T[];
get(i: number, j: number) {
return this.matrix[i * this.matrix.length + j];
}
}
However, I agree that new syntax seems convoluted.
Ran into the same case. I've been working with old Angular for a while writing long ReadonlyArray<ReadonlyArray<T>>
dreaming of the day I could update TS and use readonly
modifier only to find it will not make things much shorter for me :(
I agree with @waterplea. Now I have to use ReadonlyArray<ReadonlyArray<T>>
or readonly ReadonlyArray<T>[]
. The both options are not readable enough :(
This isn't much more readable, but readonly (readonly number[])[]
is a workaround (as seen in @Veetaha's image).
Search Terms
readonly array multi multidimensional
Suggestion
The readonly modifier should work with multidimensional arrays.
Use Cases
I assumed
readonly number[][]
to be multidimensional array which is readonly, but it translates toReadonlyArray<number[]>
, this feels like a footgun.It is currently also not very intuitive which array is readonly:
ReadonlyArray<number>[]
orReadonlyArray<number[]>
, without trying it out.Examples
I'd expect
readonly number[][]
to be equal toReadonlyArray<ReadonlyArray<number>>
Explicitely "mixed mutable" arrays could be achieved using parens:
Checklist
My suggestion meets these guidelines: