首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试使用material-ui withStyles时出现Typescript编译错误

尝试使用material-ui withStyles时出现Typescript编译错误
EN

Stack Overflow用户
提问于 2019-02-18 22:55:22
回答 1查看 3.4K关注 0票数 2

我使用的是:

  • "@material-ui/core":"3.5.1",
  • "react":"16.4.0",
  • "typescript":"2.6.1"

我正在尝试复制SimpleListMenu的material-ui演示,但我遇到了最后一个编译错误,我不知道如何处理它。

代码语言:javascript
复制
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

我得到了这个错误:

代码语言:javascript
复制
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() 中的几乎每一个例子,我得到了相同的打字错误。所以这一定和我的安装有关?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-19 03:52:31

尝试遵循https://material-ui.com/guides/typescript/中的键入模式

特别是

代码语言:javascript
复制
type Props = WithStyles<typeof styles> & {
classes: {
    root: string
}
};

没有遵循建议。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54749927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档