ReactJS typescript:类型 'number' 不可分配给类型 '0 | 8 | 16 | 24 | 32 | 40 | 不明确的'

IT技术 reactjs typescript material-ui
2021-04-30 05:33:20

我想在使用typescript的react中从 material-ui重现网格可以在此处找到现场演示

我已经调整了示例以使用typescript,这样我的 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很陌生,因此找不到正确的解决方案。

1个回答

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

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

并添加到您的导入中:

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