typescript - 类型不提供与使用 withStyles 的签名的匹配

标签 typescript material-ui

在尝试将我的 React 应用程序转换为 typescript 时,我不断收到以下错误,而且我一直无法弄清楚它在说什么。该应用程序在纯 JS 中运行良好。我正在使用 material-ui@next

 TS2345: Argument of type 'typeof ApplicationMenu' is not assignable to parameter of type 'ComponentType<AppMenuProps & WithStyles<"root" | "flex" | "menuButton" | "appBar" | "loginButton">>'.
  Type 'typeof ApplicationMenu' is not assignable to type 'StatelessComponent<AppMenuProps & WithStyles<"root" | "flex" | "menuButton" | "appBar" | "loginBu...'.
    Type 'typeof ApplicationMenu' provides no match for the signature '(props: AppMenuProps & WithStyles<"root" | "flex" | "menuButton" | "appBar" | "loginButton"> & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.

产生这个错误的代码如下

export interface AppMenuProps {
    classes: Record<string, string | undefined>
}

const styles = (theme : Theme) => ({
    root: {
        width: '100%',
    },
    flex: {
        flex: 1,
    },
    menuButton: {
        marginLeft: -12,
        marginRight: 20,
    },

    appBar: {
        background: theme.palette.common.black,
        color: theme.palette.common.white,
    },

    loginButton: {
        color: theme.palette.common.white
    }
});

class ApplicationMenu extends Component<AppMenuProps, {}> {

    render() {
        const {classes} = this.props;
        return (
            <div className={classes.root}>
                <AppBar position="static" classes={{root: classes.appBar}}>
                    <Toolbar>
                        <IconButton className={classes.menuButton} color="primary" aria-label="Menu">
                            <MenuIcon/>
                        </IconButton>
                        <Typography type="title" color="inherit" className={classes.flex}>
                            Supportworks Dashboard
                        </Typography>
                        <Button classes={{root: classes.loginButton}}>Login</Button>
                    </Toolbar>
                </AppBar>
            </div>
        );
    }
}

export default withStyles(styles)(ApplicationMenu)

最佳答案

请引用TypeScript guide ,特别是 section on withStyles :

Class components are a little more cumbersome. Due to a current limitation in TypeScript's decorator support, withStyles can't be used as a class decorator. Instead, we decorate a class component...

你需要做这样的事情:

import { WithStyles } from 'material-ui/styles';

export interface AppMenuProps {
    classes: Record<string, string | undefined>
}

const styles = (theme : Theme) => ({
    root: {
        width: '100%',
    },
    flex: {
        flex: 1,
    },
    menuButton: {
        marginLeft: -12,
        marginRight: 20,
    },
    appBar: {
        background: theme.palette.common.black,
        color: theme.palette.common.white,
    },
    loginButton: {
        color: theme.palette.common.white
    }
});

const ApplicationMenu = decorate(
  class extends React.Component<AppMenuProps & WithStyles<'root' | 'flex' | 'menuButton' | 'appBar' | 'loginButton'>> {
    render() {
      // ...
    }
  }
);

关于typescript - 类型不提供与使用 withStyles 的签名的匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026503/

相关文章:

javascript - 如何将react-show-more更多或更少文本自定义为Material-UI图标

mongoose - 从库创建新的类

typescript - 初始化 TypeScript 数组接口(interface)

javascript - Typescript 需要基于值的类型

javascript - 在 typescript Jest 上下文中, Node 配置配置是 `undefined` 吗?

javascript - "useSWR is called in function that is niether a React function component nor a custom React Hook function"错误

reactjs - 使用箭头键导航 Material-ui 列表

javascript - Angular 2函数在从API返回数据之前继续

javascript - 覆盖 Material-UI 样式不起作用

reactjs - Material UI + enzyme 测组件