在TypeScript中声明UInt8Array变量

时间:2016-08-01 21:27:42

标签: javascript typescript fs

如何在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 个答案:

答案 0 :(得分:7)

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

error TS2304: Cannot find name 'UInt8Array'

抱怨UInt8Array。但您正在寻找的名称可能是Uint8Array,小写i

答案 1 :(得分:-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);

Declare TypedArray with ArrayLike?