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

TypeScript抱怨我的setState语句正在使用属性访问器

  •  2
  • user1283776  · 技术社区  · 6 年前

    在下面的代码示例中,我尝试为多个输入使用一个onInputChange处理程序,TypeScript在语句中给出以下错误 {[name]: value} :

    [ts]
    Argument of type '{ [x: number]: string | boolean; }' is not assignable to parameter of type 'SoftwareLicenseCodesState | ((prevState: Readonly<SoftwareLicenseCodesState>, props: Readonly<SoftwareLicenseCodesProps>) => SoftwareLicenseCodesState | Pick<SoftwareLicenseCodesState, "count" | ... 4 more ... | "distributor"> | null) | Pick<...> | null'.
      Type '{ [x: number]: string | boolean; }' is not assignable to type 'Pick<SoftwareLicenseCodesState, "count" | "oneTimeUsage" | "duration" | "validFrom" | "validTo" | "distributor">'.
        Property 'count' is missing in type '{ [x: number]: string | boolean; }'.
    

    这里怎么了?我该怎么修?

    import * as React from 'react';
    import './SoftwareLicenseCodes.css';
    
    interface SoftwareLicenseCodesProps {
    }
    
    interface SoftwareLicenseCodesState {
        count: string;
        oneTimeUsage: boolean;
        duration: string;
        validFrom: string;
        validTo: string;
        distributor: string;
    }
    
    class SoftwareLicenseCodes extends React.Component<SoftwareLicenseCodesProps, SoftwareLicenseCodesState> {
        constructor(props: SoftwareLicenseCodesProps) {
            super(props);
    
            this.state = {
                distributor: '',
                count:'',
                oneTimeUsage: false,
                duration: '',
                validFrom: '',
                validTo: ''
            };
    
            this.onInputChange = this.onInputChange.bind(this);
        }
    
        handleSubmit(event: React.FormEvent<HTMLFormElement>) {
            alert('submit');
            event.preventDefault();
        }
    
        onInputChange = (event: React.FormEvent<HTMLInputElement>) => {
            const value = event.currentTarget.type === 'checkbox' ? event.currentTarget.checked : event.currentTarget.value;
    
            this.setState({
                [name]: value
            });
        }
    
        render() {
            return (
                <div className="user-container software-codes">
                    <div className="user-single-container">
                        <h1>Software License Codes</h1>
    
                        <form className="software-codes__form" onSubmit={this.handleSubmit}>
                            <label>
                                <span className="software-codes__input-element">Count</span>
                                <input
                                    name="count"
                                    type="number"
                                    value={this.state.count}
                                />
                            </label>
    
                            <label>
                                <span className="software-codes__input-element">Distributor</span>
                                <input
                                    name="distributor"
                                    type="text"
                                    value={this.state.distributor}
                                />
                            </label>
    
                            <label>
                                <span className="software-codes__input-element">One time usage</span>
                                <input
                                    name="oneTimeUsage"
                                    type="checkbox"
                                    checked={this.state.oneTimeUsage}
                                />
                            </label>
    
                            <label>
                                <span className="software-codes__input-element">Duration</span>
                                <input
                                    name="duration"
                                    type="number"
                                    value={this.state.duration}
                                />
                            </label>
                            <input className="software-codes__input-element" type="submit" value="Submit" />
                        </form>
                    </div>
                </div>
            );
        }
    }
    
    export default SoftwareLicenseCodes;
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   T.J. Crowder    5 年前

    您还没有显示错误所抱怨的接口( LicenseCodesState , Pick SoftwareLicenseCodesState .

    this.setState({[name]: value}); ,您需要做以下两件事之一:

    1. 将动态属性访问添加到定义(但继续读取):

      interface SoftwareLicenseCodesState {
          count: string;
          oneTimeUsage: boolean;
          duration: string;
          validFrom: string;
          validTo: string;
          distributor: string;
          [key: string]: any;            // <===
      }
      

      手册称之为“字符串索引签名”,它包含在 Interfaces .

      问题是,当然,这意味着你丢失了类型检查——不仅仅是在哪里使用它,基本上是在任何地方使用它 软件许可证代码状态 .

    2. setState 打电话。你有几个选择。您可以在上使用断言 name :

      const name = event.currentTarget.name as keyof SoftwareLicenseCodesState;
      this.setState({[name]: value});
      

      或者在你传递的状态对象上:

      this.setState({[event.currentTarget.name]: value} as Partial<SoftwareLicenseCodesState>);