skip to Main Content

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?

2

Answers


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

    String.fromCharCode.apply(null, [...new Uint8Array(buffer)]);
    
    Login or Signup to reply.
  2. Use parameters spread when you call the function instead of using apply (playground):

    String.fromCharCode(...new Uint8Array(buf));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search