gsharp
gsharp

Reputation: 27937

Complex Interface in TypeScript

I'm new to TypeScript and trying to implement a "complex" interface. I'm not sure wheter what I want is even possible.

I have the following javascript:

var simpleSetting = { setting1: 'test', setting2: 1 };

for that i can create an Interface like that:

interface SimpleSetting {
   setting1: string;
   setting2: number;
}

and then use it like that:

var simpleSetting: SimpleSetting = { setting1: 'test', setting2: 1 };

What I would like to do is to define an Interface for this declaration:

var setting = {
    data: {
        simpleData: {
            enable: true
        },
        view: {
            expandSpeed: ""
        }
    }
};

I know that I could create something like this:

interface View {
   expandSpeed : string;
}
interface Data {
   simpleData: SimpleData;
   view : View;
}
interface ComplexSetting {
   data : Data;
}

to achive what I want, but is it possible to achive the same result just by delcaring only one Interface instead of three?

Upvotes: 7

Views: 2995

Answers (1)

coudy
coudy

Reputation: 13958

It's certainly possible, just inline the inner interfaces like this:

interface ComplexSetting {
  data: {
    simpleData: {enable: boolean;};
    view: {expandSpeed: string;};
  };
}

Upvotes: 9

Related Questions