最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Argument of type 'Uint8Array' is not assignable to parameter of type 'number[]' - S

programmeradmin0浏览0评论

Based on crypto.subtle.exportKey("spki", cryptoKey) I want to convert the returned ArrayBuffer into a string so I can turn it into a base64 string. Based on the documentation I tried

const bufferAsString = String.fromCharCode.apply(null, new Uint8Array(buffer));

but TypeScript tells me

TS2345: Argument of type 'Uint8Array' is not assignable to parameter of type 'number[]'.

How can I fix the type errors?

Based on crypto.subtle.exportKey("spki", cryptoKey) I want to convert the returned ArrayBuffer into a string so I can turn it into a base64 string. Based on the documentation I tried

const bufferAsString = String.fromCharCode.apply(null, new Uint8Array(buffer));

but TypeScript tells me

TS2345: Argument of type 'Uint8Array' is not assignable to parameter of type 'number[]'.

How can I fix the type errors?

Share Improve this question asked Apr 7, 2023 at 19:18 baitendbidzbaitendbidz 8934 gold badges20 silver badges65 bronze badges 2
  • See stackoverflow.com/questions/53603770/… – James Commented Apr 7, 2023 at 19:21
  • Side note: <github.com/tc39/proposal-arraybuffer-base64>, if adopted, will likely make this solution obsolete. Though of course, it’d need to be polyfilled on older environments… – dumbass Commented Apr 7, 2023 at 19:55
Add a comment  | 

2 Answers 2

Reset to default 13

Use parameters spread when you call the function instead of using apply (playground):

String.fromCharCode(...new Uint8Array(buf));

You could use spread syntax or Array.from to convert the Uint8Array to a regular array.

String.fromCharCode.apply(null, [...new Uint8Array(buffer)]);
发布评论

评论列表(0)

  1. 暂无评论