【问题标题】:ReactJS Typescript: Type 'number' is not assignable to type '0 | 8 | 16 | 24 | 32 | 40 | undefined'ReactJS Typescript:类型“数字”不可分配给类型“0 | 8 | 16 | 24 | 32 | 40 |不明确的'
【发布时间】:2019-01-29 00:01:43
【问题描述】:

我想使用打字稿在反应中重现grid from material-ui。现场演示可以在here找到。

我已经调整了示例以使用打字稿,这样我的 demo.jsx 看起来像:

import { withStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import FormLabel from '@material-ui/core/FormLabel';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import RadioGroup from '@material-ui/core/RadioGroup';
import Radio from '@material-ui/core/Radio';
import Paper from '@material-ui/core/Paper';
import {WithStyles} from "@material-ui/core/styles/withStyles";

const styles = (theme:any) => ({
    root: {
        flexGrow: 1,
    },
    paper: {
        height: 140,
        width: 100,
    },
    demo: {
        height: 140,
        width: 100,
    },
    control: {
        padding: theme.spacing.unit * 2,
    },
});

class App extends React.Component<WithStyles<typeof styles>> {
    state = {
        spacing: '16',
    };

    handleChange = (key:any) => (event:any, value:any) => {
        this.setState({
            [key]: value,
        });
    };

    render() {
        const { classes } = this.props;
        const { spacing } = this.state;

        return (
            <Grid container className={classes.root} spacing={16}>
                <Grid item xs={12}>
                    <Grid container className={classes.demo} justify="center" spacing={Number(spacing)}>
                        {[0, 1, 2].map(value => (
                            <Grid key={value} item>
                                <Paper className={classes.paper} />
                            </Grid>
                        ))}
                    </Grid>
                </Grid>
                <Grid item xs={12}>
                    <Paper className={classes.control}>
                        <Grid container>
                            <Grid item>
                                <FormLabel>spacing</FormLabel>
                                <RadioGroup
                                    name="spacing"
                                    aria-label="Spacing"
                                    value={spacing}
                                    onChange={this.handleChange('spacing')}
                                    row
                                >
                                    <FormControlLabel value="0" control={<Radio />} label="0" />
                                    <FormControlLabel value="8" control={<Radio />} label="8" />
                                    <FormControlLabel value="16" control={<Radio />} label="16" />
                                    <FormControlLabel value="24" control={<Radio />} label="24" />
                                    <FormControlLabel value="32" control={<Radio />} label="32" />
                                    <FormControlLabel value="40" control={<Radio />} label="40" />
                                </RadioGroup>
                            </Grid>
                        </Grid>
                    </Paper>
                </Grid>
            </Grid>
        );
    }
}

export default withStyles(styles)(App);

问题是,它会引发以下错误:

(101,79): Type 'number' is not assignable to type '0 | 8 | 16 | 24 | 32 | 40 | undefined'.

在下面一行:

<Grid container className={classes.demo} justify="center" spacing={Number(spacing)}>

有什么想法吗?我的第一个猜测是我需要排版间距是数字类型。由于我对 typescript 很陌生,所以找不到合适的解决方案。

【问题讨论】:

    标签: reactjs typescript material-ui


    【解决方案1】:

    看起来Grid 不接受任何数字,而是一组名为GridSpacing 的受限数字。请参阅类型声明 12。因此,在像您一样转换为数字之后,您需要将该数字转换为 GridSpacing

    <Grid container className={classes.demo} justify="center" spacing={Number(spacing) as GridSpacing}>
    

    并添加到您的导入中:

    import { GridSpacing } from '@material-ui/core/Grid';
    

    【讨论】:

    • 转换为number 将基本上否定该数据类型的全部目的。这不是一个任意的数字。该枚举类型必须在整个代码中保持原样。
    • @polkovnikov.ph 看看代码在做什么:它将RadioGroup(已设置为镜像GridSpacing 选项)的字符串值转换为GridSpacing。你还打算怎么做呢?
    • 干杯!但是,上面的三个网格不是水平对齐的,而是垂直堆叠的。对此有什么想法吗?
    • @Peterhack 不,如果您自己找不到答案,请提交一个新问题。
    • 我建议不要在那里使用具有字符串值的组件。该组件显然是错误的。
    猜你喜欢
    • 2021-11-23
    • 2020-08-04
    • 1970-01-01
    • 2023-01-29
    • 2018-07-07
    • 2018-09-19
    • 2020-08-16
    • 2022-08-23
    • 2021-08-15
    相关资源
    最近更新 更多