代码之家  ›  专栏  ›  技术社区  ›  Peterhack

ReactJS Typescript:类型“number”不能分配给类型“0 | 8 | 16 | 24 | 32 | 40 |未定义”

  •  0
  • Peterhack  · 技术社区  · 6 年前

    我想复制一个 grid from material-ui here .

    我已经将示例调整为使用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)}>
    

    1 回复  |  直到 6 年前
        1
  •  0
  •   Matt McCutchen    6 年前

    看起来像 Grid 不接受任何数字,但接受 GridSpacing . 参见类型声明 1 2 . 所以在转换成一个数字之后,你需要将这个数字转换成 :

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

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