TypeScript定义,其中属性类型已知但名称为* not *

时间:2012-11-16 03:08:00

标签: kendo-ui typescript

所以我正在努力将部分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[];
        }

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

想法?提前谢谢!

1 个答案:

答案 0 :(得分:0)

除了你已经拥有的东西之外,没有办法在TypeScript中对此进行建模。