Open
Description
I would have expected a TypedArray
interface to exist in
the built-in declaration libraries. Instead, there are independent types for
Int8Array
, etc, with no common base type.
interface Int8Array { /* ... */ }
interface Int8ArrayConstructor { /* ... */ }
declare const Int8Array: Int8ArrayConstructor;
interface Uint8Array { /* ... */ }
interface Uint8ArrayConstructor { /* ... */ }
declare const Uint8Array: Uint8ArrayConstructor;
// ...
It seems sensible that there should be a common TypedArray
type, both because
- a TypeScript user might want to declare a variable as a
TypedArray
, and - it would reduce repetition in the built-in declaration files
interface TypedArray { /* ... */ }
interface Int8Array extends TypedArray { }
interface Int8ArrayConstructor { /* ... */ }
declare const Int8Array: Int8ArrayConstructor;
interface Uint8Array extends TypedArray { }
interface Uint8ArrayConstructor { /* ... */ }
declare const Uint8Array: Uint8ArrayConstructor;
// ...
Is there a good reason why there is no TypedArray
type? If not, I can submit a PR.
Use case
Consider the isTypedArray()
function from lodash.
Currently, it is declared as follows:
function isTypedArray(value: any): boolean;
This proposal would enable an appropriate type-guard, without declaring
a convoluted union type:
function isTypedArray(value: any): value is TypedArray;
Activity
mhegazy commentedon Apr 26, 2017
You can achieve the same behavior today by defining:
but in general define one TypedArray interface and use
this
types should reduce the duplication.mcmath commentedon Apr 26, 2017
@mhegazy Yes, that's the solution I'm using now, but it seems a bit unnecessary. I'll submit a PR shortly.
Add TypedArray interface
marcusjwhelan commentedon Apr 27, 2017
Would asking for types for 0x, 0b, and 0o be a different request on this? The reason I would like these type is to use with TypedArrays. Currently there is no guard on inserting any number into those typedArrays. Which shouldn't be allowed. Lets say you wanted to put a value 255 into a Int8Array field, this should have a type guard for
Something that would prevent a programmer from inserting numbers that will be converted within the typed arrays.
mcmath commentedon Apr 27, 2017
@marcusjwhelan That is a very different proposal, and far beyond the scope of this one. This is just about adding another interface to the declaration files, not about introducing new syntax.
saschanaz commentedon May 7, 2017
There already is ArrayBufferView interface in lib.d.ts for this exact purpose, which is a supertype of all typed arrays.
On Web IDL:
typedef (Int8Array or Int16Array or Int32Array or Uint8Array or Uint16Array or Uint32Array or Uint8ClampedArray or Float32Array or Float64Array or DataView) ArrayBufferView;
ulrikstrid commentedon Jun 12, 2017
The solution that @mhegazy gives might be okay in some cases. But I'm writing code that is something like this:
And it just will not compile because every has this type def (it is copied for each type of array):
zenmumbler commentedon Jun 23, 2017
FWIW, I have this concept in my library as I have to deal with TypedArrays a lot, see:
https://github.com/stardazed/stardazed/blob/master/src/core/array.ts#L36
I split it into a base and mutable and readonly extended interfaces, code as added works only in 2.4+ as the SharedArrayBuffer type was modified in the std declarations.
I also made a TS fork of gl-matrix (https://github.com/stardazed/veclib) where I often had to deal with TypedArray or array inputs being returned and wanting TS to deduce the right return type, solved like so:
This allows it to return the correct type info for both:
Refactored typed arrays.
Refactored typed arrays.
40 remaining items