首页 > 解决方案 > 获取错误参数“props”隐式具有“any”类型

问题描述

获取错误参数'props'隐式具有'any'类型。 我正在使用对材料 UI 做出反应,一切正常,但得到错误参数“道具”隐含地具有“任何”类型。我也在使用 axios API 来获取演示数据。我正在使用对材料 UI 做出反应,一切正常,但得到错误参数“道具”隐含地具有“任何”类型。我也在使用 axios API 来获取演示数据。

    import React, { Component } from 'react'
    import ApiService from "../../service/ApiService";
    import TextField from '@material-ui/core/TextField';
    import Button from '@material-ui/core/Button';
    import Typography from '@material-ui/core/Typography';

    class AddUserComponent extends Component{

        constructor(props){
            super(props);
            this.state ={
                username: '',
                password: '',
                firstName: '',
                lastName: '',
                age: '',
                salary: '',
                message: null
            }
            this.saveUser = this.saveUser.bind(this);
        }

        saveUser = (e) => {
            e.preventDefault();
            let user = {username: this.state.username, password: this.state.password, firstName: this.state.firstName, lastName: this.state.lastName, age: this.state.age, salary: this.state.salary};
            ApiService.addUser(user)
                .then(res => {
                    this.setState({message : 'User added successfully.'});
                    this.props.history.push('/users');
                });
        }

        onChange = (e) =>
            this.setState({ [e.target.name]: e.target.value });

        render() {
            return(
                <div>
                    <Typography variant="h4" style={style}>Add User</Typography>
                    <form style={formContainer}>

                        <TextField type="text" placeholder="username" fullWidth margin="normal" name="username" value={this.state.username} onChange={this.onChange}/>

                        <TextField type="password" placeholder="password" fullWidth margin="normal" name="password" value={this.state.password} onChange={this.onChange}/>

                        <TextField placeholder="First Name" fullWidth margin="normal" name="firstName" value={this.state.firstName} onChange={this.onChange}/>

                        <TextField placeholder="Last name" fullWidth margin="normal" name="lastName" value={this.state.lastName} onChange={this.onChange}/>

                        <TextField type="number" placeholder="age" fullWidth margin="normal" name="age" value={this.state.age} onChange={this.onChange}/>

                        <TextField type="number" placeholder="salary" fullWidth margin="normal" name="salary" value={this.state.salary} onChange={this.onChange}/>

                        <Button variant="contained" color="primary" onClick={this.saveUser}>Save</Button>
                </form>
        </div>
            );
        }
    }
    const formContainer = {
        display: 'flex',
        flexFlow: 'row wrap'
    };

    const style ={
        display: 'flex',
        justifyContent: 'center'

    }

    export default AddUserComponent;

标签: reactjsreact-native

解决方案


无需删除 tsconfig.json 中的 'noImplicitAny' - 您可以显式地为 props 赋予将删除错误的 'any' 类型:

        constructor(props: any){

(来源:https ://www.tsmean.com/articles/learn-typescript/no-implicit-any-best-practice/ )


推荐阅读