【问题标题】:Typescript compile error trying to use material-ui withStyles打字稿编译错误尝试使用material-ui withStyles
【发布时间】:2019-07-11 23:50:36
【问题描述】:

我正在使用:

  • "@material-ui/core": "3.5.1",
  • “反应”:“16.4.0”,
  • “打字稿”:“2.6.1”

我正在尝试复制 SimpleListMenu 的 material-ui 演示,但我遇到了最后一个编译错误,我不知道该怎么办。

import React from 'react';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import MenuItem from '@material-ui/core/MenuItem';
import Menu from '@material-ui/core/Menu';
import {StyleRulesCallback, Theme, WithStyles} from "@material-ui/core";
import withStyles from "@material-ui/core/styles/withStyles";

const styles: StyleRulesCallback<"root"> = (theme: Theme) => ({
root: {
    width: '100%',
    maxWidth: 360,
    backgroundColor: theme.palette.background.paper,
},
});

const options = [
'Show some love to Material-UI',
'Show all notification content',
'Hide sensitive notification content',
'Hide all notification content',
];

type Props = WithStyles<typeof styles> & {
classes: {
    root: string
}
};

type State = {
anchorEl: EventTarget & HTMLElement | null
selectedIndex: number
};

class SimpleListMenu extends React.Component<Props, State> {
state = {
    anchorEl: null,
    selectedIndex: 1,
};

handleClickListItem = (event: React.MouseEvent<HTMLElement>) => {
    this.setState({ anchorEl: event.currentTarget });
};

handleMenuItemClick = (event: React.MouseEvent<HTMLElement>, index: number) => {
    this.setState({ selectedIndex: index, anchorEl: null });
};

handleClose = () => {
    this.setState({ anchorEl: null });
};

render() {
    const { anchorEl } = this.state;

    return (
    <div className={this.props.classes.root}>
        <List component="nav">
        <ListItem
            button
            aria-haspopup="true"
            aria-controls="lock-menu"
            aria-label="When device is locked"
            onClick={this.handleClickListItem}
        >
            <ListItemText
            primary="When device is locked"
            secondary={options[this.state.selectedIndex]}
            />
        </ListItem>
        </List>
        <Menu
        id="lock-menu"
        anchorEl={anchorEl}
        open={Boolean(anchorEl)}
        onClose={this.handleClose}
        >
        {options.map((option, index) => (
            <MenuItem
            key={option}
            disabled={index === 0}
            selected={index === this.state.selectedIndex}
            onClick={event => this.handleMenuItemClick(event, index)}
            >
            {option}
            </MenuItem>
        ))}
        </Menu>
    </div>
    );
}
}
export default withStyles(styles, {withTheme: true })<Props>(SimpleListMenu); // Compile error here

我收到此错误:

TS2344: Type 'Props' does not satisfy the constraint 'ComponentType<ConsistentWith<Props, boolean>>'. 
 Type 'Props' is not assignable to type 'StatelessComponent<ConsistentWith<Props, boolean>>'.
  Type 'Props' provides no match for the signature '(props: ConsistentWith<Props, boolean> & {children? ReactNode;}, context?: any): ReactElement<any> | null'.

我已经尝试了很多方法来解决这个问题。我主要关注这个guy's example。有人知道吗?

ETA:我尝试​​编译 stackoverflow 问题中的代码,但得到的编译错误与我的编译错误相同。我还尝试了Typescript type error in component returned by withStyles() 中的几乎所有示例,并且我得到了相同的打字稿错误。所以它一定与我的安装有关?

【问题讨论】:

    标签: reactjs typescript material-ui


    【解决方案1】:

    尝试按照https://material-ui.com/guides/typescript/的打字模式

    特别是

    type Props = WithStyles<typeof styles> & {
    classes: {
        root: string
    }
    };
    

    不遵循建议。

    可以在https://next--material-ui.netlify.com/demos/menus/#selected-menus 上找到菜单演示的工作示例(单击显示源,然后切换到 TS)

    【讨论】:

    • 我已经阅读了打字稿指南,但遇到了编译问题。与菜单演示代码一样,我对这一行有疑问:interface Props extends WithStyles&lt;typeof styles&gt; {}。我的编译器抱怨:一个接口只能扩展一个类或另一个接口。
    • 有什么问题。 WithStyles 导出为接口。我需要查看您的项目设置(package.json、tsconfig.json 和完整文件)以了解问题所在。
    • 在我的文件@material-ui/core/styles/withStyles.ds 中,WithStyles 的定义类似于:export type WithStyles&lt;,所以它是一个类型。
    • 我刚刚浏览了这些版本,而自 v1.1.0 以来,WithStyles 就不再是一个界面。它在 v1.2.0 中以某种方式更改为 type
    • 这应该没关系。可能是旧打字稿版本的问题。您可以尝试升级到 2.9 还是最好升级到 3.1?
    猜你喜欢
    • 2021-08-01
    • 2018-06-23
    • 2020-02-24
    • 2016-08-21
    • 2017-06-26
    • 2019-06-27
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多