// Component.js
interface Props {
children: React.ReactNode
}
const Parent: React.FC<Props> & { Item?: React.FC } = ({ children }) => {
return <div>{children}</div>;
}
const Child: React.FC<Props> = () => {
return <span>Child</span>;
}
Parent.Item = Child;
export default Parent;
// App.js
import { default as Parent } from 'component.js';
const { Item } = Parent;
const App = () => {
return (
<Parent>
<Item/> {/* Typescript error here */}
<Item/> {/* Typescript error here */}
<Item/> {/* Typescript error here */}
</Parent>
);
}
发生这种误差是因为打字稿将其视为可选的,导致其不确定。修复程序是键入为
Item
Parent
用作有效的反应组件。