Typescript Typscript:Type';Uint8Array';类型';中缺少以下属性;编号[]';:

Typescript Typscript:Type';Uint8Array';类型';中缺少以下属性;编号[]';:,typescript,google-chrome-extension,Typescript,Google Chrome Extension,我一直在从事chrome扩展项目,在使用“typescript”时,将Uint8Array解析为字符串时遇到问题(我在没有typescript的情况下测试了相同的代码,并且没有出现错误) chrome.webRequest.onBeforeRequest.addListener( 请求侦听器, {url:[']},['requestBody'] ); 函数请求侦听器(详细信息:任意){ let id:number=details.requestId; 让url:string=details.u

我一直在从事chrome扩展项目,在使用“typescript”时,将Uint8Array解析为字符串时遇到问题(我在没有typescript的情况下测试了相同的代码,并且没有出现错误)

chrome.webRequest.onBeforeRequest.addListener(
请求侦听器,
{url:[']},['requestBody']
);
函数请求侦听器(详细信息:任意){
let id:number=details.requestId;
让url:string=details.url;
let方法:string=details.method;
让正文:字符串=“”
let headers:Header[]=[];
如果(details.method==“POST”){
body=decodeURIComponent(String.fromCharCode.apply(null,新的Uint8Array(details.requestBody.raw[0].bytes));//Uint8Array(+4个重载)
8位无符号整数值的类型化数组。内容初始化为0。如果无法分配请求的字节数,则引发异常。
“Uint8Array”类型的参数不能分配给“number[]”类型的参数。
类型“Uint8Array”缺少类型“number[]”中的以下属性:pop、push、concat、shift和3个以上的.ts(2345)

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

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

我在Typescript游戏场中尝试了这一点,它似乎在编译:
String.fromCharCode.apply(null,[…new Uint8Array()])
。我不知道这是否适用于您,但我认为Typescript希望您将Uint8Array转换为普通数组。@cdimitroulas谢谢!现在它工作起来很有魅力!如果您发布了答案,我会选择一个答案。我会添加一个答案,以便您可以这样做:)
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.apply(
  null,
  [...new Uint8Array(details.requestBody.raw[0].bytes)]
)