简体   繁体   English

是否可以使用 React + Typescript 在 object 阵列内扩展接口?

[英]Is it possible to extend an interface inside an object array with React + Typescript?

I have two interfaces:我有两个接口:

Interface 1:接口一:

interface ILineItemProps {
    state: 'OPENED' | 'COMPLETED' | 'CANCELLED' | 'PENDING';
}

Interface 2:接口二:

interface IProps {
    steps: [
        {
            key: string;
            title: string;
        }
    ];
}

Interface 1 must add its object, in this case the state, inside the object of interface 2, thus:接口 1 必须在接口 2 的 object 内添加其 object,在本例中为 state,因此:

[{
key: string;
title: string;
state: interface1
}]

I've tried it in several ways:

[{
key: string;
title: string;
} & Interface1]

Array<{
key: string;
title: string;
} & Interface1>

But I could not.但我不能。

Try this:尝试这个:

interface ILineItemProps {
  state: 'OPENED' | 'COMPLETED' | 'CANCELLED' | 'PENDING';
}

type Step = {
  key: string;
  title: string;
} & ILineItemProps;

interface IProps {
  steps: Step[];
}

// test:

const iProps = {
  steps: [] as Step[],
};

iProps.steps.push({
  key: 'a',
  title: 'b',
  state: 'OPENED',
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM