是否有一种方法通过道具传递一个界面?



我想知道是否有一种方法可以通过prop传递接口

例如,下面的组件试图向下传递一个接口到Apple组件。

import Apple from "./";
Interface A {
name: string;
}
const Component = () => {
return (
<Apple 
...
type={A} <--- Pass it in like this
/>
);
}

1-以上可以做到吗?

2-如果是,Apple组件内部的接口类型是什么?

您不能通过props传递接口,因为它只存在于构建时,而不存在于运行时。如果你想在某处消磨时间,你可以使用泛型。

export interface AppleProps<T> { /* ... */ }
export function Apple<T>(props: AppleProps<T>) { /* ... */ }
// ...
import Apple from "./";
interface A {
name: string;
}
const Component = () => {
return (
// You can provide generic arguments for react elements
<Apple<A> />
);
}

相关内容

最新更新