流嵌套对象值不可访问

时间:2018-02-02 11:32:17

标签: reactjs flowtype

我正在尝试从对象访问深层嵌套值,但我在流程中遇到以下错误:

Property cannot be accessed on property 'author' of unknown type

type ARTICLE_TYPE = {
  id: number,
  authorId: number,
  type: 'article' | 'video' | 'audio' | 'perspective',
  title: string,
  preview: string,
  imageUrl: ?string,
  date: string,
}

type AUTHOR_TYPE = {
  company: string,
  id: number,
  name: string,
  profileImage: string
}

type TileProps = {
  ...ARTICLE_TYPE,
  ...{
    author: AUTHOR_TYPE,
  },
  imageAspectRatio: string
}

我怀疑这可能与我定义类型的方式有关,但对我来说似乎没问题:

相关的可测试代码为Here

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我建议你不要使用带有流类型的扩展运算符,而应使用类型连接。

type TileProps = ARTICLE_TYPE & {
  author: AUTHOR_TYPE,
  imageAspectRatio: string
}

让自己和那些在您之后阅读代码的人轻松生活。