首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在fromCharCode.apply 3中正确使用fromCharCode.apply与Uint8Array?

如何在fromCharCode.apply 3中正确使用fromCharCode.apply与Uint8Array?
EN

Stack Overflow用户
提问于 2018-12-03 23:59:20
回答 2查看 1.9K关注 0票数 3

因此,我继承的一些代码如下所示:String.fromCharCode.apply(null, new Uint8Array(license));最近,我们必须更新项目依赖项,而不是TypeScript 3,它抱怨代码与此消息不正确:Argument of type 'Uint8Array' is not assignable to parameter of type 'number[]'. Type 'Uint8Array' is missing the following properties from type 'number[]': pop, push, concat, shift, and 3 more.,我还有其他几个地方有相同的错误,它们都是Uint8Array,除了一个Uint16Array。问题似乎在于对具有多个重载的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)));,这两种方法都无法重新创建代码的原始函数,但它们确实抑制了错误消息。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-04 02:49:30

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

代码语言:javascript
复制
let jsonKey: string = "";
(new Uint8Array(license)).forEach(function (byte: number) {
    jsonKey += String.fromCharCode(byte);
});
票数 8
EN

Stack Overflow用户

发布于 2022-05-20 01:53:51

您的第一次尝试几乎成功;您只需显式指定泛型参数:

代码语言:javascript
复制
String.fromCharCode.apply(null, Array.from<number>(new Uint8Array(license)));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53603770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档