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

TypeScript 3:类型中缺少属性

  •  1
  • BenjiFB  · 技术社区  · 6 年前

    import * as React from 'react';
    
    
    class ExampleComponent extends React.Component<{}, {
            firstName: string, lastName: string
        }> {
        clearState() {
            this.setState({
                firstName: "",
                lastName: ""
            });
        }
    
        constructor(props) {
            super(props);
            this.state = {
                firstName: '', lastName: ''
            };
    
            this.handleChange = this.handleChange.bind(this);
        }
    
        handleChange(event) {
           //***** This is the line that gives me an error ******
            this.setState({ [event.target.id]: event.target.value });
    
        }
        public render() {
    
            return <form>
    
                <div className="form-group">
                    <div className="row">
                        <div className="col">
                            <label className="sr-only">First Name</label>
                            <input type="text" className="form-control name-element" id="firstName"
                                placeholder="First Name" value={this.state.firstName} onChange={this.handleChange} required={true} />
                        </div>
    
                        <div className="col">
                            <label className="sr-only">Last Name</label>
                            <input type="text" className="form-control name-element" id="lastName"
                                placeholder="Last Name" value={this.state.lastName} onChange={this.handleChange} required={true} />
                        </div>
                    </div>
                </div>
    
            </form>
    
        }
    }
    
    // Wire up the React component to the Redux store
    export default ExampleComponent;
    

    Error   TS2345  (TS) Argument of type '{ [x: number]: any; }' is not assignable to parameter of type '{ firstName: string; lastName: string; } | ((prevState: Readonly<{ firstName: string; lastName: string; }>, props: {}) => { firstName: string; lastName: string; } | Pick<{ firstName: string; lastName: string; }, "firstName" | "lastName">) | Pick<...>'.
      Type '{ [x: number]: any; }' is not assignable to type 'Pick<{ firstName: string; lastName: string; }, "firstName" | "lastName">'.
        Property 'firstName' is missing in type '{ [x: number]: any; }'.
    

    如有任何建议,将不胜感激。

    2 回复  |  直到 6 年前
        1
  •  5
  •   Daniel Khoroshko    6 年前

    Record firstname lastname . 什么时候? event.target.id string ,尽管在你的情况下 number 因为某种原因。

    一种方法是把它扔给 any 喜欢(更新):

    handleChange(event) {
        this.setState({ [event.target.id]: event.target.value } as any);
    }
    

        2
  •  2
  •   Dakota    6 年前

    我认为要获得更可靠的类型检查,可以为每个输入添加一个处理程序,如下所示

    function makeHandler(
        id: WhatEnumYourIdIs
    ): (event: React.SyntheticEvent<HTMLInputElement>) => void {
        return function(event: React.SyntheticEvent<HTMLInputElement>): void{
            this.setState({ id: target.value });
        }
    }
    

    然后添加一个处理程序

    <input onChange={this.makeHandler(Enum.FirstName)} />
    

    如果依赖输入id,则不能保证state字段匹配,它可能会编译,但实际上不会进行类型检查。

    推荐文章