Type使用withStyles为签名提供不匹配项

问题描述 投票:3回答:1

在尝试将我的React应用程序转换为打字稿时,我不断收到下面的错误,对于我的生活,我无法弄清楚它的含义。该应用程序在普通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 material-ui
1个回答
4
投票

请参考TypeScript guide,特别是section on withStyles

类组件有点麻烦。由于current limitation in TypeScript's decorator supportwithStyles不能用作类装饰器。相反,我们装饰一个类组件......

你需要做这样的事情:

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() {
      // ...
    }
  }
);
© www.soinside.com 2019 - 2024. All rights reserved.