【问题标题】:Typescript interface for array object数组对象的打字稿接口
【发布时间】:2020-03-29 11:30:46
【问题描述】:

我目前正在接受我的组件的接口,它接受数组对象“选项”作为其参数之一。我的问题是如何为数组对象创建接口。我相信您需要对接口使用索引签名,但我之前没有使用过,不知道它需要如何结构。

目前我使用箭头功能。这是我声明函数接口的方式

   interface IOption {
         key: number;
        text: string;
        value: number
    }
    Interface DropDownInputInt {
              name: string;
              value: any;
              label: string;
              disabled: boolean;
              onChange: Function;
              extraClassName: string;
              required: boolean;
              options: IOption[] | string;
              multiple: boolean;
              clearable: boolean;
              index?: number;
              placeholder: string;
              toolTipMessage: string;
              addCollon: boolean;
}

const DropDownInput = ({
              name,
              value,
              label,
              disabled,
              onChange,
              extraClassName,
              required,
              options,
              multiple,
              clearable,
              index,
              placeholder,
              toolTipMessage,
              addCollon
}: DropDownInputInt) => {
            //MY CODES HERE
})

我的第二个问题是如何在接受字符串和对象的接口中创建。

【问题讨论】:

标签: javascript reactjs typescript


【解决方案1】:

你的意思是这样的吗?

interface IOption {
  key: number;
  text: string;
  value: number; // or any
}

function func(options: IOption[]) {}

但是如果您接受其他参数,您如何在接口上声明它?我希望有一种方法,无需将所有内容都声明为内联

我对 React.js 不是很熟悉,但通常你应该可以使用 TypeScript 来做到这一点

interface DropDownInputInt {
  name: string;
  value: any;
  label: string;
  // bla bla ...
}


const DropDownInput = (dropdownOptions: DropDownInputInt) => {
  // your code here
}

至于你的其他问题

我的第二个问题是如何在接受字符串和对象的接口中创建。

这是一个例子(见管道|):

interface DropDownInputInt {
  somethindThatAcceptsBothStringAndObject: string | object
  // bla bla ...
}

我试图将您的答案合并到我的代码中,请参阅上面的更改,但不知何故,它在我的函数体内抛出错误,说“类型'字符串 | IOption' 上不存在属性'值'。当我尝试向下钻取时options.value

好的,根据您的DropDownInputInt 接口,您接受options 属性的stringIOption[] 类型。由于您有两种不同的类型,您应该像这样检查options 类型:

const DropDownInput = (dropdownOptions: DropDownInputInt) => {
  // ...
  if(typeof dropdownOptions.options === 'string') {
    // you have a string here
  } else if(Array.isArray(dropdownOptions.options) && dropdownOptions.options.length > 0) {
    // you have IOption array here, which is not empty
    dropdownOptions.options[0].value; // accessing the value of the first option
    dropdownOptions.options[1].value; // accessing the value of the second option
    // etc. or simply use a for-loop to evaluate the options
  }
}

【讨论】:

  • 但是如果您接受其他参数,您如何在接口上声明它?我希望有一种方法,无需将所有内容都声明为内联
  • @Billy 更新了我的答案。
  • 我试图将您的答案合并到我的代码中,请参阅上面的更改,但不知何故,它在我的函数体内抛出错误,提示“类型'string | IOption'上不存在属性'值'。当我尝试深入了解 options.value
  • @Billy 您的意思是“string | IOption[] 类型上不存在属性'值',还是真的是string | IOption?原因,在您的DropDownInputInt 中有string | IOption[]
猜你喜欢
  • 2019-09-28
  • 2017-02-26
  • 1970-01-01
  • 2021-07-24
  • 2023-01-05
  • 2020-08-10
  • 1970-01-01
  • 2021-11-05
  • 2017-10-27
相关资源
最近更新 更多