Microsoft/TypeScript

Suggestion: a built-in TypedArray interface

Open

#15.402 geöffnet am 26. Apr. 2017

Auf GitHub ansehen
 (24 Kommentare) (47 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Domain: lib.d.tsFix AvailableHelp WantedSuggestion

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

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;

Contributor Guide