带有 TS 的 makeStyles 中的主题和道具

问题描述

发现了很多类似的问题,但没有一个能完全回答这个问题。您如何将 themeprops 传递给 Material UI makeStyles 钩子,但从最外层的闭包访问它们而不会发生 TS 爆炸?像这样:

type StyleProps = {
  post: Post;
}

const useStyles = makeStyles<Theme,StyleProps>((theme: Theme,props: StyleProps) => ({
    root: {
      maxWidth: '100%',backgroundImage: ({ post }) => post.mainImage
    },date: {
      margin: theme.spacing(1),marginLeft: theme.spacing(2)
    },heroimage: {
      maxWidth: '100%',height: 'auto',objectFit: 'cover'
    }
}))

TS 错误

Type '(theme: Theme,props: any) => { root: (props: any) => Cssproperties; expandable: (props: any) => Cssproperties; iconContainer: { position: string; top: number; right: number; display: string; }; expandMoreIcon: (props: any) => Cssproperties; bannerContent: (props: any) => Cssproperties; }' is not assignable to type 'StyleRulesCallback<Theme,StyleProps,string>'.

我可以像您在 root 中看到的那样从样式块内联访问 props,但这不是我要找的。​​p>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)