TS2322类型'字符串'不能分配给'keyof T'类型

时间:2018-06-07 11:45:29

标签: typescript

升级到TS 2.9.0后出现以下错误

[at-loader] ./packages/elc-form/src/views/Form.tsx:39:9 
 TS2322: Type '{ form: { onChange: (ev: FormEvent<HTMLInputElement>) => void; onBlur: (name: string) => void; va...' is not assignable to type 'IContext'.
  Types of property 'form' are incompatible.
   Type '{ onChange: (ev: FormEvent<HTMLInputElement>) => void; onBlur: (name: string) => void; values: T;...' is not assignable to type 'IFormProps'.
     Types of property 'getError' are incompatible.
      Type '<K extends keyof T>(name: K) => IFormErrors<T>[K]' is not assignable to type '(fieldName: string) => string'.
       Types of parameters 'name' and 'fieldName' are incompatible.
        Type 'string' is not assignable to type 'keyof T'. 

在Form.tsx中

export interface IFormProps {
    onChange(ev: React.FormEvent<HTMLInputElement>): void;
    onBlur(fieldName: string): void;
    values: IFormValues;
    getError(fieldName: string): string;
    onDropDownChange(fieldName: string, value: string): void;
    setFieldValidations(fieldName: string, validations?: {}): void;
    removeValidationField(fieldName: string): void;
}
export interface IContext {
    form: IFormProps;
}
...
public getChildContext(): IContext {
    return {
        form: {
            onChange: this.onChange,
            onBlur: this.onBlur,
            values: this.store.values,
            getError: this.store.getError,
            onDropDownChange: this.onDropDownChange,
            setFieldValidations: this.store.setFieldValidations,
            removeValidationField: this.store.removeValidationField
        }
    };
}

并在FormStore.tsx中:

public getError = <K extends keyof IFormValues>(name: K) => {
    return this.touched[name] && this.errors[name];
};

根据TS 2.9.0,似乎类型不再匹配 你能提供一些指导吗? 提前谢谢。

0 个答案:

没有答案
相关问题