简体   繁体   English

Typescript中数组对象的对象

[英]Object of objects of arrays in Typescript

I have a structure like that and need to make a definitions of types / interfaces for that but I can't make this working properly. 我有一个这样的结构,需要为此定义类型/接口,但是我无法使其正常工作。

layoutsSet: {
       1: {
        "lg": [
            {
                i: "1",
                x: 0,
                y: 0,
                w: 2,
                h: 2
            },
            {
                i: "2",
                x: 2,
                y: 0,
                w: 2,
                h: 2
            },
            {
                i: "3",
                x: 4,
                y: 0,
                w: 2,
                h: 2
            },
        ]
    }
};

Thats how it looks like for now. 那就是现在的样子。 It needs some changes because in compilation Im getting errors: 它需要进行一些更改,因为在编译时出现错误:

TS2322: Type '{ "lg": { i: string; TS2322:输入'{“ lg”:{i:字符串; x: number; x:数字; y: number; y:数字; w: number; w:数字; h: number; h:数字; }[]; } []; }' is not assignable to type 'Layouts'. }”不可分配给“布局”类型。 Property 'md' is missing in type '{ "lg": { i: string; 类型'{“ lg”中缺少属性'md':{i:string; x: number; x:数字; y: number; y:数字; w: number; w:数字; h: number; h:数字; }[]; } []; }'. }”。

export interface Layout {
    i: string;
    x: number;
    y: number;
    w: number;
    h: number;
}

export enum Breakpoint {
    LG = "lg",
    MD = "md",
    SM = "sm",
    XS = "xs",
    XXS = "xxs"
}

export interface LayoutBreakpoint extends Array<Layout> {}

export type Layouts = {
    [b in Breakpoint]: LayoutBreakpoint
}

export interface LayoutsSet {
    [index: number]: Layouts
}

What should i change here to make it works? 我应该在这里进行哪些更改才能使其正常工作? Can anyone help me please? 谁能帮我吗?

// EDIT //编辑

The best part is that when Ive changed 最好的部分是当我改变时

export type Layouts = {
    [key: string]: LayoutBreakpoint
}

It is working now... 现在正在运作...

But why cant I define index as an enum type? 但是,为什么不能将索引定义为枚举类型呢?

in interface LayoutsSet you are defining index as a number type. interface LayoutsSet您将index定义为number类型。 the variable names cannot be of type number . 变量名不能是number类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM