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
创建一个类,以使代码更整洁。