【问题标题】:TypeScript definition where property type is known but the name is *not*属性类型已知但名称为 *not* 的 TypeScript 定义
【发布时间】:2012-11-04 18:39:02
【问题描述】:

所以我正在努力将部分 Kendo UI 库转换为 TypeScript,但我被困在 Model 类上。问题是指定字段的方式。

给定以下数组:

 function someModelFields() {
    return {
        Id: { editable: false, nullable: false, defaultValue: emptyGuid },
        ScenarioId: { editable: false, nullable: false, defaultValue: emptyGuid },
        UnitId: { editable: false, nullable: false },
        UnitNumber: { type: "string", editable: false },
        SquareFootage: { type: "number", editable: false }
    };
};

...我将如何在 TypeScript 中对此进行建模,其中属性名称可以是任何东西,但属性的类型是已知的?这是我目前所拥有的:

export module Kendo {

    export module Data {

        export class FieldTypeEnum {
            static string: string;
            static number: string;
            static boolean: string;
            static date: string;
        }

        export class ValidationDefinition {
            required: bool;
        }

        export class FieldDefinition {
            defaultValue: string;
            editable: bool;
            nullable: bool;
            parse: () => any;
            type: FieldTypeEnum;
            validation: ValidationDefinition;
        }

        export class ModelDefinition {
            id: string;
            fields: any[];
        }

我知道“字段”的定义是错误的,并且我知道我在 ModelDefinition.fields 和 FieldDefinition 类型的动态属性名之间缺少一个类型。

想法?提前致谢!

【问题讨论】:

    标签: kendo-ui typescript


    【解决方案1】:

    除了现有的之外,没有其他方法可以在 TypeScript 中对此进行建模。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      相关资源
      最近更新 更多