Reactjs 将TypeScript类型作为道具传递给React组件?
是否可以将TypeScript类型作为道具传递给React组件Reactjs 将TypeScript类型作为道具传递给React组件?,reactjs,typescript,Reactjs,Typescript,是否可以将TypeScript类型作为道具传递给React组件 export type ActivitiesType = { RUN: "RUN"; WALK: "REST"; ROUNDS: "ROUNDS"; }; <MyComponent activity={ActivitiesType.RUN} /> Ritaj是对的,Enum可以做到这一点: enum ActivitiesType { RUN: "RUN"; WALK: "REST"; ROUN
export type ActivitiesType = {
RUN: "RUN";
WALK: "REST";
ROUNDS: "ROUNDS";
};
<MyComponent activity={ActivitiesType.RUN} />
Ritaj是对的,Enum可以做到这一点:
enum ActivitiesType {
RUN: "RUN";
WALK: "REST";
ROUNDS: "ROUNDS";
}
type Props = {
type: ActivitiesType;
}
我不确定这是否可能,因为类型在运行时不存在。为什么不在活动中使用字段
类型
?更多信息来自官方文档为什么不使用enum?您正在寻找泛型,请阅读这篇文章,可能是重复的:。您的类型本质上是一个环境枚举。您需要一个在运行时存在的真实版本。
enum ActivitiesType {
RUN: "RUN";
WALK: "REST";
ROUNDS: "ROUNDS";
}
type Props = {
type: ActivitiesType;
}