typescript - 如何在 TypeScript 3 中正确使用 fromCharCode.apply 和 Uint8Array?

标签 typescript ecmascript-6 uint8array

所以我继承了一些代码,如下所示: String.fromCharCode.apply(null, new Uint8Array(license)); 最近,我们不得不更新项目依赖项,并且我们不在 TypeScript 3 上,它提示代码不正确并显示以下消息: 'Uint8Array' 类型的参数不可分配给'number[]' 类型的参数。 “Uint8Array”类型缺少“number[]”类型的以下属性:pop、push、concat、shift 和另外 3 个。 我有几个其他地方也有同样的错误,除了一个是 Uint16Array 之外,它们都是 Uint8Array。问题似乎在于对具有多个重载的 Uint8Array 构造函数进行了一些更改。我尝试将代码更改为 const jsonKey: string = String.fromCharCode.apply(null, Array.from(new Uint8Array(license))); const jsonKey: string = String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(license))); 这些都没有重新创建代码的原始功能,但它们确实抑制了错误消息。

最佳答案

你应该能够做一些更容易阅读的事情,即使不是那么紧凑:

let jsonKey: string = "";
(new Uint8Array(license)).forEach(function (byte: number) {
    jsonKey += String.fromCharCode(byte);
});

关于typescript - 如何在 TypeScript 3 中正确使用 fromCharCode.apply 和 Uint8Array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53603770/

相关文章:

javascript - Java 将 byte[] 转换为 BigInteger

javascript - 在 ES6 中是否允许解构类实例属性?

c# - 如何在 Node.js WebSocket 服务器和 C# 客户端之间交换二进制数据?

c - 如何将 double 数据转换为 uint8 数组并再次返回 double

javascript - react typescript 元素类型

javascript - babel obj symbol.iterator 未定义

javascript - 用 Babel 扩展 MediaSource ——如何正确调用 super()?

javascript - 将嵌套 Knockout.js 组件与 RequireJs 结合使用

javascript - 按键过滤 map

javascript - 使用 vanilla javascript 隐藏包含特定文本的标签