So I have some code that I inherited that looks like this:
String.fromCharCode.apply(null, new Uint8Array(license));
Recently, we had to update the project dependencies and we are not on TypeScript 3 which complains that the code is not correct with this message:
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.
I have a few other places with the same errors, and they are all Uint8Array except one that is a Uint16Array. The issue seems to be with some changes to the Uint8Array constructor that has several overloads. I have tried changing the code to
const jsonKey: string = String.fromCharCode.apply(null, Array.from(new Uint8Array(license)));
and
const jsonKey: string = String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(license)));
Neither of these has worked to recreate the original function of the code, but they did suppress the error messages.
Asked
Active
Viewed 2,374 times
3

ahetman
- 1,274
- 11
- 17
-
Can you share a sample 'license' value? This might be what you're looking for: https://stackoverflow.com/a/29676964/4375436. – Tim Ellison Dec 04 '18 at 00:59
-
I will get a sample license shortly, however the solution you linked is the equivalent of the one I tried with `Array.prototype.slice.call` that didn't work. – ahetman Dec 04 '18 at 17:00
2 Answers
7
You should be able to do something which will be easier to read even if not as compact:
let jsonKey: string = "";
(new Uint8Array(license)).forEach(function (byte: number) {
jsonKey += String.fromCharCode(byte);
});

user2479438
- 414
- 4
- 4
1
Your first attempt almost worked; you just need to explicitly specify the generic parameter:
String.fromCharCode.apply(null, Array.from<number>(new Uint8Array(license)));

Mu-Tsun Tsai
- 2,348
- 1
- 12
- 25