React 样式组件作为 属性 TypeScript 问题

React styled component as a property TypeScript issue

考虑以下示例:

const StyledComponent = styled.div` ... `;

interface ComponentProps {
  styledComponent: any;
}

const Component: React.FC<ComponentProps> = () => { ... };

Component.styledComponent = StyledComponent;

export default Component;

我用它来引用其他组件中组件的样式组件。按预期工作,但我一直在抱怨 TypeScript:

Property 'styledComponent' does not exist on type 'FC<ComponentProps>'.

我在这里错过了什么?

您需要定义组件:

const Component: React.FC<ComponentProps> & { styledComponent: any } = () => { ... };