question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: a built-in TypedArray interface

See original GitHub issue

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;

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:34
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
mhegazycommented, Apr 26, 2017

You can achieve the same behavior today by defining:

type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array;

but in general define one TypedArray interface and use this types should reduce the duplication.

9reactions
patrickrobertscommented, Oct 16, 2018

@dschnelldavis new doesn’t work because it’s essentially an abstract constructor, for lack of a better terminology. However, instanceof does work as demonstrated in this snippet:

const TypedArray = Object.getPrototypeOf(Uint8Array)
console.log(new Uint8Array() instanceof TypedArray)

As other people have said, even though it’s an invisible constructor, there should still be an interface for it, as it encapsulates a lot of functionality in a single base class and drastically reduces repetition for definition files, as well as more accurately reflecting the specification.

I think there should also be an interface for TypedArrayConstructor as well, since there are common static properties, so something like this:

interface TypedArray {
  ...
}

interface TypedArrayConstructor {
  readonly prototype: TypedArray;
  ...
}

interface Float32Array extends TypedArray { }
interface Float64Array extends TypedArray { }
interface Int16Array extends TypedArray { }
interface Int32Array extends TypedArray { }
interface Int8Array extends TypedArray { }
interface Uint16Array extends TypedArray { }
interface Uint32Array extends TypedArray { }
interface Uint8Array extends TypedArray { }
interface Uint8ClampedArray extends TypedArray { }

interface Float32ArrayConstructor extends TypedArrayConstructor { }
interface Float64ArrayConstructor extends TypedArrayConstructor { }
interface Int16ArrayConstructor extends TypedArrayConstructor { }
interface Int32ArrayConstructor extends TypedArrayConstructor { }
interface Int8ArrayConstructor extends TypedArrayConstructor { }
interface Uint16ArrayConstructor extends TypedArrayConstructor { }
interface Uint32ArrayConstructor extends TypedArrayConstructor { }
interface Uint8ArrayConstructor extends TypedArrayConstructor { }
interface Uint8ClampedArrayConstructor extends TypedArrayConstructor { }
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: "TypedArray" interface for Typed Arrays
typescript doesn't have a TypedArray interface but like the suggestion here, you can use ArrayBufferView which is a supertype of all typed ...
Read more >
TypedArray.prototype.at() - JavaScript - MDN Web Docs
The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers.
Read more >
Typescript: "Typedarray" Interface For Typed Arrays - ADocLib
Create an empty typed array of an interface or class; How to create empty typed ... subarray polymorphic Suggestion: a builtin TypedArray interface...
Read more >
Crypto | Node.js v19.3.0 Documentation
getRandomValues(typedArray); crypto.hkdf(digest, ikm, salt, info, keylen, ... As a legacy interface, it is possible to create new instances of the crypto.
Read more >
"toLocaleString" | Can I use... Support tables for HTML5, CSS3 ...
JavaScript built-in: TypedArray: toLocaleString · Global · Chrome · Edge * · Safari · Firefox · Opera · IE · Chrome for Android....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found