从 TypeScript 1.0 升级到 1.8 后 IgniteUI 中缺少属性
Missing properties in IgniteUI after upgrade from TypeScript 1.0 to 1.8
我们正在将 TypeScript 项目从 1.0 版升级到 TypeScript 1.8。
我们现在还使用最新的 IgniteUI 定义文件 (v 16.1),该文件(在 Typescript 版本更改之后)不适用于我们现有的对象初始化(请参阅此 post 底部的错误)。
这是我们现有的使用 IgniteUI igTextEditor 的 TypeScript 代码:
$(this.textinput).igTextEditor({
maxLength: this.maxChars,
textMode: sTextMode,
listItems: [""],
buttonType: "dropdown",
dropDownListOpening: function (evt, ui) {
formBase.setActiveForm(self.formID);
self.buttonClicked();
return false;
},
// Validator Options
validatorOptions: {
onblur: true,
onchange: false,
required: this.required,
notificationOptions: {
direction: "right",
showIcon: "true",
mode: "popover"
},
custom: function (value, fieldOptions) {
if (self.showError) {
self.showError = false;
if (self.errorMessage.length > 0) {
$(this.element).igValidator("option", "errorMessage", self.errorMessage);
}
return false;
}
self.validate(value);
return true;
}
},
keyup: function (evt, ui) { if (evt.keyCode == 13) { $(evt.currentTarget).blur() } },
focus: function () { formBase.setActiveForm(self.formID) }
});
这是来自igniteui.d.ts的相关接口定义:
interface IgTextEditor {
textMode?: string;
maxLength?: number;
includeKeys?: string;
excludeKeys?: string;
toUpper?: boolean;
toLower?: boolean;
listMatchIgnoreCase?: boolean;
listMatchOnly?: boolean;
listMatchContains?: boolean;
listAutoComplete?: boolean;
}
interface JQuery {
igTextEditor(options: IgTextEditor): JQuery;
igTextEditor(optionLiteral: string, options: IgTextEditor): JQuery;
igTextEditor(optionLiteral: string, optionName: string, optionValue: any): JQuery;
igTextEditor(optionLiteral: string, optionName: string): any;
igTextEditor(methodName: string): any;
}
此代码与 igniteui.d.ts 相比的唯一变化来自:
igTextEditor(optionLiteral: string, optionName: any, optionValue: any): JQuery;
至:
igTextEditor(optionLiteral: string, optionName: string, optionValue: any): JQuery;
升级到 TypeScript 1.8 后,我们收到以下错误:
error TS2345: 构建: 类型为 '{ [x: number]: undefined; 的参数最大长度:数字;文本模式:字符串;列表项:字符串[]; buttonType: s...' 不可分配给 'string'.
类型的参数
问题:考虑到 TypeScript 在 1.8 中的类型验证更加严格,并且转换为 不是一个选项,社区建议的最佳处理方式是什么这个情况?
好的
在打字稿 1.6 中添加了 "Strict object literal assignment checking"。
这意味着您不能将不符合接口的参数对象作为参数传递!!EXACTLY!!
因此,如果参数对象接口声明字段 {field1, field2} - 您只能传递 {field1, field2} 而不是 {field1} 或 {field1, field2, field3}
示例:
var obj: { id: 数字};
obj = {id: 1, name: "my object"} - 将会出错 - 因为 'name' 未在 obj 声明中定义。
要传递额外的字段需要使用索引器
var obj: { id: 数字, [x:string] 任意};
您可以传递任何其他字段
只要记住严格类型并检查所有对象是否 100% 符合接口(可能是过时的 IgniteUI 使用的东西)
我们正在将 TypeScript 项目从 1.0 版升级到 TypeScript 1.8。 我们现在还使用最新的 IgniteUI 定义文件 (v 16.1),该文件(在 Typescript 版本更改之后)不适用于我们现有的对象初始化(请参阅此 post 底部的错误)。
这是我们现有的使用 IgniteUI igTextEditor 的 TypeScript 代码:
$(this.textinput).igTextEditor({
maxLength: this.maxChars,
textMode: sTextMode,
listItems: [""],
buttonType: "dropdown",
dropDownListOpening: function (evt, ui) {
formBase.setActiveForm(self.formID);
self.buttonClicked();
return false;
},
// Validator Options
validatorOptions: {
onblur: true,
onchange: false,
required: this.required,
notificationOptions: {
direction: "right",
showIcon: "true",
mode: "popover"
},
custom: function (value, fieldOptions) {
if (self.showError) {
self.showError = false;
if (self.errorMessage.length > 0) {
$(this.element).igValidator("option", "errorMessage", self.errorMessage);
}
return false;
}
self.validate(value);
return true;
}
},
keyup: function (evt, ui) { if (evt.keyCode == 13) { $(evt.currentTarget).blur() } },
focus: function () { formBase.setActiveForm(self.formID) }
});
这是来自igniteui.d.ts的相关接口定义:
interface IgTextEditor {
textMode?: string;
maxLength?: number;
includeKeys?: string;
excludeKeys?: string;
toUpper?: boolean;
toLower?: boolean;
listMatchIgnoreCase?: boolean;
listMatchOnly?: boolean;
listMatchContains?: boolean;
listAutoComplete?: boolean;
}
interface JQuery {
igTextEditor(options: IgTextEditor): JQuery;
igTextEditor(optionLiteral: string, options: IgTextEditor): JQuery;
igTextEditor(optionLiteral: string, optionName: string, optionValue: any): JQuery;
igTextEditor(optionLiteral: string, optionName: string): any;
igTextEditor(methodName: string): any;
}
此代码与 igniteui.d.ts 相比的唯一变化来自:
igTextEditor(optionLiteral: string, optionName: any, optionValue: any): JQuery;
至:
igTextEditor(optionLiteral: string, optionName: string, optionValue: any): JQuery;
升级到 TypeScript 1.8 后,我们收到以下错误:
error TS2345: 构建: 类型为 '{ [x: number]: undefined; 的参数最大长度:数字;文本模式:字符串;列表项:字符串[]; buttonType: s...' 不可分配给 'string'.
类型的参数问题:考虑到 TypeScript 在 1.8 中的类型验证更加严格,并且转换为
好的 在打字稿 1.6 中添加了 "Strict object literal assignment checking"。 这意味着您不能将不符合接口的参数对象作为参数传递!!EXACTLY!!
因此,如果参数对象接口声明字段 {field1, field2} - 您只能传递 {field1, field2} 而不是 {field1} 或 {field1, field2, field3}
示例:
var obj: { id: 数字}; obj = {id: 1, name: "my object"} - 将会出错 - 因为 'name' 未在 obj 声明中定义。
要传递额外的字段需要使用索引器
var obj: { id: 数字, [x:string] 任意}; 您可以传递任何其他字段
只要记住严格类型并检查所有对象是否 100% 符合接口(可能是过时的 IgniteUI 使用的东西)