我有一个withStyles
中的material-ui
定义的样式化组件。我正在使用typescript
,因此它抱怨最后一行的类型错误。原因是组件ServiceListComponent
需要classes
和services
,而withStyles
仅提供classes
。 services
将由父组件提供。如何在withStyle
语句中使类型正确?
const styles = createStyles({...})
interface Props {
services: RootState.ServicesState;
classes: { [className in keyof typeof styles]: string };
}
const ServiceListComponent = ({ classes, services }: Props) => {
return (
<Grid container className={classes.root}>
<Grid container className={classes.parent} spacing={8}>
{itemsArray.map(value => (
<Grid key={value} item className={classes.item} xs={4} sm={4} md={3} xl={4}>
<Paper className={classes.paper} />
</Grid>
))}
</Grid>
</Grid>
);
};
ServiceListComponent.propTypes = {
classes: PropTypes.object.isRequired,
};
export const ServiceList = withStyles(styles)(ServiceListComponent);
以上代码的错误在最后一行:
TS2345: Argument of type '{ ({ classes, services }: Props): Element; propTypes: { classes: Validator<object>; }; }' is not assignable to parameter of type 'ComponentType<ConsistentWith<Props, { classes: Record<"root" | "parent" | "paper" | "items" | "item", string>; }>>'.
Type '{ ({ classes, services }: Props): Element; propTypes: { classes: Validator<object>; }; }' is not assignable to type 'FunctionComponent<ConsistentWith<Props, { classes: Record<"root" | "parent" | "paper" | "items" | "item", string>; }>>'.
Types of property 'propTypes' are incompatible.
Type '{ classes: Validator<object>; }' is not assignable to type 'WeakValidationMap<ConsistentWith<Props, { classes: Record<"root" | "parent" | "paper" | "items" | "item", string>; }>>'.
Types of property 'classes' are incompatible.
Type 'Validator<object>' is not assignable to type 'Validator<{ root: string; parent: string; paper: string; items: string; item: string; }>'.
Type '{}' is missing the following properties from type '{ root: string; parent: string; paper: string; items: string; item: string; }': root, parent, paper, items, item
我可以通过如下添加any
使其正确,但是我不想使用任何内容。
export const ServiceList = withStyles(styles)<any>(ServiceListComponent);