【问题标题】:Why does flowtype complain about my array definition in my reducer?为什么 flowtype 抱怨我的 reducer 中的数组定义?
【发布时间】:2018-04-29 22:25:32
【问题描述】:

这是我对瞳孔对象的定义:

type PupilType = {
  classId: string,
  created: number,
  description: string,
  firstname: string,
  gender: string,
  id: string,
  lastname: string,
  updated: number,
};

这是我的瞳孔redux reducer,瞳孔保存在一个数组中:

export default function reducer(state: Array<Object> = [], action: Object) 
{
    ...
}

这行得通,但如果不只是说数组包含对象,我会尝试使用这样的类型:

export default function reducer(state: Array<PupilType> = [], action: Object) 
{
    ...
}

flowtype 说有错误:

“数组字面量此类型与数组类型的预期参数类型不兼容”

这是为什么?我可以使用

Array<PupilType> 

在我的应用程序的其他地方就好了。它似乎只在减速器中引起了问题。

【问题讨论】:

  • 可能是因为您定义了一个空的默认值,但您的类型显示了一个填充的数组。试试(state: Array&lt;PupilType&gt; = [pupil] ...
  • 我收到no errors...。也许您可以以同样的方式分享错误链接。
  • 呃,如果没有工作示例,很难诊断出这个问题。你能把它修剪成相关的流类型并在flow.org/try上发帖吗?
  • 一个(可能是错误的)在黑暗中拍摄:也许看看 $ReadOnlyArray,一个只读版本的数组。有时这可以解决我在使用数组时遇到的问题。

标签: reactjs redux flowtype reducers


【解决方案1】:

试试:

export default function reducer(state: Array<PupilType>, action: Object) 
{
    ...
}

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 2015-10-08
    • 2012-05-15
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多