gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:44 25 4
gpt4 key购买 nike

在尝试将我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com