打字稿:类型 'Uint8Array' 缺少类型 'number[]' 的以下属性:

Typscript: Type 'Uint8Array' is missing the following properties from type 'number[]':

我一直致力于 chrome 扩展项目,当我使用 'typescript' 时,将 Uint8Array 解析为字符串时遇到问题(我在没有打字稿的情况下测试了相同的代码,并且没有出现错误)。

chrome.webRequest.onBeforeRequest.addListener(
        requestListener,
        { urls: ["<all_urls>"] }, ['requestBody']
);
function requestListener(details: any) {
            let id: number = details.requestId;
            let url: string = details.url;
            let method: string = details.method;
            let body: string = ''
            let headers: Header[] = [];
            if (details.method == "POST") {
                body = decodeURIComponent(String.fromCharCode.apply(null,  new Uint8Array(details.requestBody.raw[0].bytes))); // <-- claims error
            }
          
        }
    }

}

错误信息是..

var Uint8Array: Uint8ArrayConstructor
new (elements: Iterable<number>) => Uint8Array (+4 overloads)
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the requested number of bytes could not be allocated an exception is raised.

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.ts(2345)

问题是 String.fromCharCode 函数需要一个数字数组 (number[]) 作为参数,因此您需要在传递之前将 Uint8Array 转换为一个数字数组它。

String.fromCharCode.apply(
  null,
  [...new Uint8Array(details.requestBody.raw[0].bytes)]
)