【发布时间】:2020-04-10 16:21:54
【问题描述】:
我很难创建一个代表组件状态的界面。 假设如下:
interface IFoo {
fooName: string;
fooValue: string | boolean | Array<string>
}
那么我们有以下对象:
const objOne: IFoo = {
fooName: 'objOneName',
fooValue: true
}
const objTwo: IFoo = {
fooName: 'objTwoName',
fooValue: ['a', 'b', 'c']
}
现在我们有了一个包含这些对象的数组:
const myArray: IFoo[] = [objOne, objTwo];
现在在一个 React 组件中,我收到了 myArray 作为道具,需要为组件状态构造接口。
我需要一个新的接口来表示 React 组件的状态,它应该如下所示:
interface myState {
someProp: string;
// The following values need to be dynamic. I don't know how many entries are there in the array
// in the is example: objOneName is the value of objOne[fooName] and it's type is the type of obj[fooValue]
objOneName: boolean,
objTwoName: Array<string>
}
【问题讨论】:
标签: reactjs typescript typescript-typings typescript-generics