在TypeScript中声明UInt8Array变量

7
在TypeScript中如何声明UInt8Array类型的函数参数?
import * as fs from "fs";
fs.readFile(fileName, (err: string, data: UInt8Array) => {
                if (err) {
                    return console.error(err);
                }
                console.log("file text: " + data.toString());
            });

我遇到了一个错误:

错误 TS2304:找不到名称“UInt8Array”

谢谢

2个回答

13

您的错误消息表示存在拼写错误,即

error TS2304: Cannot find name 'UInt8Array'

抱怨UInt8Array。 但是您要寻找的名称可能是小写字母Uint8Array


1
以下实例适用于TypeScript 1.6(撰写时的最新稳定版本):
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);

使用ArrayLike声明TypedArray?


2
我不理解这个答案。我有Typescript 2.2.2,和OP一样遇到了同样的问题。阅读了这个答案后,我离解决问题还是一样的远。 - Rich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接