【问题标题】:Missing properties in IgniteUI after upgrade from TypeScript 1.0 to 1.8从 TypeScript 1.0 升级到 1.8 后,IgniteUI 中缺少属性
【发布时间】:2016-12-28 06:44:04
【问题描述】:

我们正在将 TypeScript 项目从 1.0 版升级到 TypeScript 1.8。 我们现在还使用最新的 IgniteUI 定义文件 (v 16.1),该文件(在 Typescript 版本更改之后)不适用于我们现有的对象初始化(请参阅本文底部的错误)。

这是我们现有的使用 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 后,我们收到以下错误:

错误 TS2345:构建:类型参数 '{ [x: number]: undefined;最大长度:数字;文本模式:字符串;列表项:字符串[]; buttonType: s...' 不能分配给“string”类型的参数。

问题:考虑到 TypeScript 在 1.8 中对类型验证更加严格,并且不能选择强制转换为 ,社区会建议处理这种情况的最佳方法是什么?

【问题讨论】:

  • 您使用的是哪个版本的 Ignite UI?
  • Ignite UI 16.1 版 - 现在也更新了问题。

标签: javascript typescript1.8 ignite-ui


【解决方案1】:

好的 在 typescript 1.6 中添加了“严格的对象文字赋值检查”。 这意味着你不能作为不符合接口的参数对象传递!!EXACTLY!!

所以如果参数对象接口声明字段 {field1, field2} - 你可以只传递 {field1, field2} 但不能传递 {field1} 或 {field1, field2, field3}

例子:

var obj: { id: number }; obj = {id: 1, name: "my object"} - 将是一个错误 - 因为 'name' 未在 obj 声明中定义。

要传递额外的字段需要使用索引器

var obj: { id: number, [x:string] any }; 并且您可以传递任何其他字段


只需记住严格类型并检查所有对象是否 100% 满足接口(可能是从过时的 IgniteUI 中使用的东西)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    相关资源
    最近更新 更多