我正在创建一个“Columns”组件。应该只需要一个或两个 child 。
如何限制使用 Typescript 的 child 数量?
最佳答案
这似乎有效
import * as React from "react";
type ChildType = React.ReactElement;
interface ColumnProps {
children: [ChildType, ChildType] | ChildType; // Either 2 of this type or one
}
const Column: React.FC<ColumnProps> = ({ children }) => {
return <div>{children}</div>;
};
export default function App() {
return (
<div className="App">
<Column>
<div>Test 1</div>
<div>Test 2</div>
</Column>
</div>
);
}
关于reactjs - 在 React 中,如何使用 Typescript 限制子进程的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732451/