【发布时间】:2021-09-04 08:06:01
【问题描述】:
我正在编写一个快速包装界面。我正在寻找一些神秘类型:
让 Foo 成为一些 React 组件,接受 FooProps 类型的 props。
让 X 成为代表任何通用 React 组件或任何 HTML 元素的接口。哪里有Foo extends X,还有div extends X。 X 是可以被<X></X> 转换成 JSX.Element 的任何东西
让 Y<T extends X> 成为我们T 的道具,这又是任何元素,例如Foo 或div。其中Y<Foo> = FooProps 和Y<div> = { className: string, ... }
我们可以这样写:
interface WrapperProps<T extends X>{
children?: React.ReactNode;
attributes?: Y<T>;
}
那么我们可以这样写:
const props: WrapperProps<T> = ...;
const el: JSX.Element = (
<T {...attributes}>
{ children }
</T>
);
X和Y这些神秘类型是什么?
【问题讨论】:
标签: reactjs typescript jsx