Ulad Melekh
Ulad Melekh

Reputation: 944

Declare UInt8Array variable in TypeScript

How to declare function parameter of type UInt8Array in TypeScript?

import * as fs from "fs";
fs.readFile(fileName, (err: string, data: UInt8Array) => {
                if (err) {
                    return console.error(err);
                }
                console.log("file text: " + data.toString());
            });

I'm having an error:

error TS2304: Cannot find name 'UInt8Array'

Thanks

Upvotes: 7

Views: 13384

Answers (2)

Frederick Nord
Frederick Nord

Reputation: 1304

Your error message indicates a typo, i.e.

error TS2304: Cannot find name 'UInt8Array'

complains about UInt8Array. But the name you're looking for is probably Uint8Array, with a lowercase i.

Upvotes: 14

Leroy Thompson
Leroy Thompson

Reputation: 480

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);

Declare TypedArray with ArrayLike?

Upvotes: 1

Related Questions