typescript - TS2322 类型“字符串”不可分配给类型“keyof T”
问题描述
升级到 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,类型似乎不再匹配,您能否提供一些指导?提前致谢。
解决方案
推荐阅读
- python - 将连续的 True 转换为 False python
- c++ - boost::geometry::Polygon 和“有向”折线的交集
- ios - AppsFlyer GoogleTagManager 集成 - 不同数量的事件
- nhibernate - 找不到类中属性的设置器
- pentaho - 核心桥在 ActiveMQ Artemis 中不起作用
- django - Django导入导出:尝试读取文件CSV时遇到错误
- javascript - jQuery ajax 不保持数组的顺序
- c++11 - 如何使用 boost::aio::async_connect 和 lambda
- tensorflow - 区分磁盘模型的类型
- vue.js - 我的 VueJS 应用程序未呈现 - 我查看了有关堆栈溢出的其他解决方案,但似乎都没有解决我的问题