Suggestion: a built-in TypedArray interface
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:34
- Comments:20 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You can achieve the same behavior today by defining:
but in general define one TypedArray interface and use
thistypes should reduce the duplication.@dschnelldavis
newdoesn’t work because it’s essentially an abstract constructor, for lack of a better terminology. However,instanceofdoes work as demonstrated in this snippet: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
TypedArrayConstructoras well, since there are common static properties, so something like this: