7

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

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

1 Comment

I don't understand this answer. I have Typescript 2.2.2 and I'm seeing the same issue as the OP. I am no closer to a solution after reading this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.