我正在使用redux创建一个应用程序。我用的是打字稿和immutable.js。当我对类型化对象使用不可变的方法(如updateIn() )时,它会抛出错误。
以下是我尝试过的代码:
interface aType{
id: number
}
function randFun(a:aType){
a = a.updateIn([....]); //It is throwing error in this line of code.
}
randFun({id:2});属性'updateIn‘不存在于{.}类型中。
我怎样才能消除这个错误?
发布于 2015-12-16 08:34:40
至于TypeScript,这是因为updateIn没有在aType接口中定义。要解决这个问题,您可以编写:
interface aType{
id: number,
updateIn(n: number): aType
}
function randFun(a:aType){
let n = 5;
a = a.updateIn(n); // No error here
}
randFun({id:2}); // This still need to be fixed!但是,它也不能在JavaScript中工作,因为您不能在{id:2}对象上调用updateIn。
https://stackoverflow.com/questions/34306280
复制相似问题