如何在material-ui的样式化组件中指定其他道具?

时间:2019-04-23 23:17:26

标签: reactjs typescript material-ui

我有一个withStyles中的material-ui定义的样式化组件。我正在使用typescript,因此它抱怨最后一行的类型错误。原因是组件ServiceListComponent需要classesservices,而withStyles仅提供classesservices将由父组件提供。如何在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);

0 个答案:

没有答案