【问题标题】:ReactJs / Typescript How to pass an array of objects into interface props?ReactJs / Typescript 如何将对象数组传递给界面道具?
【发布时间】:2017-01-26 09:53:03
【问题描述】:

我有两个这样的界面。

export interface TableBoxPropsHeader{
    name: string,
    isIconOnly: boolean
}

export interface TableBoxProps<T> {
 headerNames: TableBoxPropsHeader[],
 // some stuff
}

我正在尝试使用此定义创建一个变量,但由于某种原因它说我正在尝试传递 string[] 数组而不是 TableBoxPropsHeader[] 数组。

private tableBoxProps: TableBoxProps<SomeType> = {
  headerNames: [{name: "Name", isIconOnly:false}, {name: "Category", isIconOnly:true}],

我正在使用 VSCode,它不会抱怨上述问题。但是 npm 会打印以下内容

Types of property 'headerNames' are incompatible.
  Type 'string[]' is not assignable to type 'TableBoxPropsHeader[]'
    Type 'string' is not assignable to type 'TableBoxPropsHeader'.

我做错了什么?如何创建接口数组?

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    好吧,这对我来说有点愚蠢。另一个文件中有另一个变量出现错误。现在可以正常使用了……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2020-10-10
      • 2019-08-06
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多