Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 打字稿:“;不可分配给never”类型的参数;错误_Typescript - Fatal编程技术网

Typescript 打字稿:“;不可分配给never”类型的参数;错误

Typescript 打字稿:“;不可分配给never”类型的参数;错误,typescript,Typescript,我得到了这个密码: blockingFiles: []; //this is on the class //this is inside a function of the class this.blockingFiles = []; this.blockingFiles.push({file: blockingFile, id: fileId}); 但是我在推到数组中的对象中的文件和id上出现了一个错误。将阻止文件声明为空数组时,您没有真正指定数组元素的类型,这就是为什么会出现错误的原因

我得到了这个密码:

blockingFiles: []; //this is on the class


//this is inside a function of the class
this.blockingFiles = [];
this.blockingFiles.push({file: blockingFile, id: fileId});

但是我在推到数组中的对象中的
文件和
id上出现了一个错误。

阻止文件
声明为空数组时,您没有真正指定数组元素的类型,这就是为什么会出现错误的原因

一种解决方案是定义对象的接口(包含“file”和“id”键的接口),然后将
blockingFiles
变量声明为该对象的数组

你可以这样做:

interface IBlockingFiles {
    file: string; // Replace the type here if it make sense to your code
    id: string; // Replace the type here if it make sense to your code
}

blockingFiles: IBlockingFiles[];

当将
blockingFiles
声明为空数组时,实际上并没有指定数组元素的类型,因此会出现错误

一种解决方案是定义对象的接口(包含“file”和“id”键的接口),然后将
blockingFiles
变量声明为该对象的数组

你可以这样做:

interface IBlockingFiles {
    file: string; // Replace the type here if it make sense to your code
    id: string; // Replace the type here if it make sense to your code
}

blockingFiles: IBlockingFiles[];

谢谢,我尝试过将
对象[]
的第一行改为
[]
,效果很好,但是我将为
阻止文件创建一个类,使代码更整洁。谢谢,我尝试过将
对象[]
的第一行改为
[]
,效果很好,但是,我将为
BlockingFile
创建一个类,以使代码更整洁。