Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare TypedArray with ArrayLike?

Tags:

typescript

I was looking for a way to declare generic TypedArray types in a d.ts file. TypedArray doesn't seem to exist for some reason but I came across a comment somewhere that suggested using ArrayLike<T>. Would this still be the best solution?

I can make a guess at what ArrayLike is, but is there any documentation for it? Googling and searching the Typescript site doesn't turn up much.

EDIT: I just noticed that typed array constructors are declared to take an ArrayLike as the first parameter, so that would indicate this is the right way to go.

like image 444
pixelmike Avatar asked Oct 25 '15 16:10

pixelmike


1 Answers

I'm not sure if I understand your question correctly but the following instantiations work in TypeScript 1.6 (the latest stable version at the time of writing):

let t01 = new Uint8Array([1, 2, 3, 4]);
let t02 = new Int8Array([1, 2, 3, 4]);  
let t03 = new Uint8Array([1, 2, 3, 4]);
let t04 = new Uint8ClampedArray([1, 2, 3, 4]);
let t05 = new Int16Array([1, 2, 3, 4]);
let t06 = new Uint16Array([1, 2, 3, 4]);
let t07 = new Int32Array([1, 2, 3, 4]);
let t08 = new Uint32Array([1, 2, 3, 4]);
let t09 = new Float32Array([1.5, 2.5, 3.5, 4.5]);
let t10 = new Float64Array([1.5, 2.5, 3.5, 4.5]);

let arrayBuffer = new ArrayBuffer(16);

(TypeScript playground with the previous script, MDN documentation)

You can see that typed arrays are in es6.d.ts file:

/**
  * A typed array of 8-bit integer values. The contents are initialized to 0. If the requested 
  * number of bytes could not be allocated an exception is raised.
  */
interface Int8Array {
    /** 
      * Returns an array of key, value pairs for every entry in the array
      */
    entries(): IterableIterator<[number, number]>;
    /** 
      * Returns an list of keys in the array
      */
    keys(): IterableIterator<number>;
    /** 
      * Returns an list of values in the array
      */
    values(): IterableIterator<number>;
    [Symbol.iterator](): IterableIterator<number>;
}

Is this what you are after?

EDIT:

lib.es6.d.ts defines ArrayLike (see definition of what is Array-like in JavaScript) as:

interface ArrayLike<T> {
    length: number;
    [n: number]: T;
}

and Iterable as

interface Iterable<T> {
    [Symbol.iterator](): Iterator<T>;
}

This is how a function very similar to yours is defined:

interface Uint8ArrayConstructor {

    //
    // ...
    // 

    /**
      * Creates an array from an array-like or iterable object.
      * @param arrayLike An array-like or iterable object to convert to an array.
      * @param mapfn A mapping function to call on every element of the array.
      * @param thisArg Value of 'this' used to invoke the mapfn.
      */
    from(arrayLike: ArrayLike<number> | Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array;
}

so I would define your function as:

function foo(arrayLike: ArrayLike<number> | Iterable<number>) { ... }
like image 197
Martin Vseticka Avatar answered Oct 16 '22 12:10

Martin Vseticka