Search Terms
ReadonlyTypedArray Int8Array Uint8Array Uint16Array Int16Array Uint32Array Int32Array Float32Array Float64Array
Suggestion
It would be nice if we had readonly or otherwise immutable typed arrays.
Use Cases
I would like to return my array directly for performant bulk access to the data within, but make sure only the methods on the object itself are used to alter it.
Examples
Ideally it'd be just like ReadonlyArray, in that you'd return ReadonlyTypedArray
I tried the following workaround:
export type ReadonlyTypedArray<T> = T | {
readonly [n: number]: number;
} ;
This, as expected, only has the indexing available, none of the methods/fields available in T are accessible (and if you use & instead of |, it doesn't override the indexing operator)
My current workaround is to do this:
export interface ReadonlyUint16Array extends Uint16Array {
readonly [n: number]: number;
};
Like ReadonlyArray, this would only be a static readonly enforcement, not runtime.
Checklist
My suggestion meets these guidelines:
Search Terms
ReadonlyTypedArray Int8Array Uint8Array Uint16Array Int16Array Uint32Array Int32Array Float32Array Float64Array
Suggestion
It would be nice if we had readonly or otherwise immutable typed arrays.
Use Cases
I would like to return my array directly for performant bulk access to the data within, but make sure only the methods on the object itself are used to alter it.
Examples
Ideally it'd be just like ReadonlyArray, in that you'd return ReadonlyTypedArray
I tried the following workaround:
This, as expected, only has the indexing available, none of the methods/fields available in T are accessible (and if you use & instead of |, it doesn't override the indexing operator)
My current workaround is to do this:
Like ReadonlyArray, this would only be a static readonly enforcement, not runtime.
Checklist
My suggestion meets these guidelines: