Using the interface to enter an anonymous object in typescript

I have an IBase interface and a variable that contains several other objects (in the example, I just added a base for a better demonstration)

interface IBase {
    height?:number;
    width?:number;
}

var element = {
    base: {

    }
}

How can I say that an object that has a varable element.base element is of type IBase? I know that I can create a type for an element variable that contains database types, etc., but it is also possible to enter this script without this.

+4
source share
3 answers

Van den Brink has a good answer. Just like a demonstration of another option:

var element = {
    base: <IBase> {

    }
}

It will also give the desired intelligence:

enter image description here

+11
source

. , .

interface IBase {
    height?:number;
    width?:number;
}

, {}. .. . , .

, :

var x: IBase = {
    a: 'efsdsdf',
    v: 'sdfdsf' 
};

, , , IBase.

- , element.

interface IElement {
    base: {
        height?:number;
        width?:number;
    }
}

var element: IElement = {
    base: {
        height: 4
    }
}
+4

var , , :

var element: { base: IBase; } = {
    base: {
    }
}

. , :    IElement {       : IBase;   } , : var: IElement = {base: {}}

+3
source

Source: https://habr.com/ru/post/1544913/


All Articles