Typescript 类型';上不存在属性id;绝不';

Typescript 类型';上不存在属性id;绝不';,typescript,redux,reducers,Typescript,Redux,Reducers,我正试图用Typescript在Redux中执行一个.map函数到一个状态数组中,问题是它抛出了一个错误 [ts]类型“从不”上不存在属性“id” 在landing.idif语句中,由于它是一个对象数组,所以代码对我来说很有意义,但似乎缺少了一些东西 export default function landingReducer (state = [], action) { switch(action.type) { case ActionTypes.ADD_LANDING: retu

我正试图用Typescript在Redux中执行一个
.map
函数到一个状态数组中,问题是它抛出了一个错误

[ts]类型“从不”上不存在属性“id”

landing.id
if语句中,由于它是一个对象数组,所以代码对我来说很有意义,但似乎缺少了一些东西

export default function landingReducer (state = [], action) {
switch(action.type) {
  case ActionTypes.ADD_LANDING:
    return [...state, action.landing]
  case ActionTypes.EDIT_LANDING:
    return state.map(landing => {
      if (action.landing.id == landing.id) {
        return landing;
      }
    })

提前谢谢

您的代码缺少括号和其他必要的导入,这使得其他人很难快速完成

也就是说,TypeScript将
状态
参数推断为空数组文本的类型
[]
,它被认为是
从不[]
,这意味着它将始终为空。因此,
map
函数不起作用,因为
landing
被推断为不可能的值(没有类型为
never
的有效值)

如果要修复它,应该告诉TypeScript可能是哪种类型的数组
状态
。快速修复方法是将其设置为
any
的数组:

... function landingReducer (state: any[] = [], action) ...
理想情况下,您应该向参数中添加更具体的类型,以便TypeScript可以帮助您捕获错误(您如何知道
action
具有
type
属性?)


希望有帮助;祝你好运

这可能是由于
状态
和/或
操作
参数缺少类型造成的。尝试以下代码,该代码应能与TypeScript 2.4+一起使用,灵感来自:

接口登陆{
id:任何;
}
枚举操作类型{
ADD_LANDING=“ADD_LANDING”,
EDIT_LANDING=“EDIT_LANDING”,
其他行动=“任何其他行动类型”
}
接口AddLandingAction{
类型:ActionTypes.ADD\u LANDING;
着陆:着陆;
}
接口编辑操作{
类型:ActionTypes.EDIT\u着陆;
着陆:着陆;
}
类型着陆作用=
|AddLandingAction
|编辑作用;
功能landingReducer(状态:Landing[],操作:LandingAction){
开关(动作类型){
案例ActionTypes.ADD_着陆:
返回[…状态、操作、着陆]
案例操作类型。编辑\着陆:
返回状态.map(landing=>{
if(action.landing.id==landing.id){
返回着陆;
}
});
}
}