[英]Use one or another interface type in Typescript/React
Having this type:有这种类型:
export interface IMyTypes {
First: {
name: string;
city: string;
country: string;
status: string;
};
Second: {
name: string;
age: number;
height: number;
};
}
This must be used in a component but I cannot make it accept both, the props should be First or Second.这必须在组件中使用,但我不能让它同时接受,道具应该是第一或第二。
I can make it work for First:我可以让它为 First 工作:
import { IMyTypes } from '../my-types';
interface MyComponentProps {
componentProps: ComponentProps<IMyTypes['First']>;
}
or for the second:或第二个:
interface MyComponentProps {
componentProps: ComponentProps<IMyTypes['Second']>;
}
But doesn't work to make it accept one or the other, tried like the following but it isn't correct:但不能让它接受一个或另一个,尝试如下但它不正确:
interface MyComponentProps {
componentProps: ComponentProps<IMyTypes['First' | 'Second']>;
}
Is there a solution to this?有针对这个的解决方法吗?
The solution that solves the issue:解决问题的解决方案:
interface MyComponentProps {
componentProps:
| ComponentProps<IMyTypes['First']>
| ComponentProps<IMyTypes['Second']>;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.