【问题标题】:Declare UInt8Array variable in TypeScript在 TypeScript 中声明 UInt8Array 变量
【发布时间】:2016-12-07 02:21:13
【问题描述】:

如何在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”

谢谢

【问题讨论】:

    标签: javascript typescript fs


    【解决方案1】:

    您的错误消息表明一个错字,即

    error TS2304: Cannot find name 'UInt8Array'
    

    抱怨UInt8Array。但是您要查找的名称可能是Uint8Array,带有小写的i

    【讨论】:

      【解决方案2】:

      以下实例在 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?

      【讨论】:

      • 我不明白这个答案。我有 Typescript 2.2.2,我看到与 OP 相同的问题。阅读此答案后,我离解决方案更近了。
      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2021-01-18
      • 2020-02-10
      • 2019-01-04
      相关资源
      最近更新 更多