Skip to content

Suggestion: a built-in TypedArray interface #15402

Description

@mcmath

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.

interfaceInt8Array{/* ... */}interfaceInt8ArrayConstructor{/* ... */}declareconstInt8Array: Int8ArrayConstructor;interfaceUint8Array{/* ... */}interfaceUint8ArrayConstructor{/* ... */}declareconstUint8Array: 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
interfaceTypedArray{/* ... */}interfaceInt8ArrayextendsTypedArray{}interfaceInt8ArrayConstructor{/* ... */}declareconstInt8Array: Int8ArrayConstructor;interfaceUint8ArrayextendsTypedArray{}interfaceUint8ArrayConstructor{/* ... */}declareconstUint8Array: 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:

functionisTypedArray(value: any): boolean;

This proposal would enable an appropriate type-guard, without declaring
a convoluted union type:

functionisTypedArray(value: any): value is TypedArray;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFix AvailableA PR has been opened for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions