Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 804
gsharp Avatar asked Jun 04 '14 15:06

gsharp


People also ask

What are interfaces in TypeScript?

An Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, means a class which implements an interface is bound to implement all its members.

Can TypeScript implement multiple interfaces?

An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.

How do you use extended interface TypeScript?

Extending Interfaces in TypeScript # Use the extends keyword to extend interfaces in TypeScript, e.g. interface Dog extends Animal {age: number;} . The extends keyword allows us to copy the members from other named types and add new members to the final, more generic interface. Copied!

Is TypeScript interface struct?

Interface is a structure that defines the contract in your application. It defines the syntax for classes to follow.


1 Answers

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

interface ComplexSetting {
  data: {
    simpleData: {enable: boolean;};
    view: {expandSpeed: string;};
  };
}
like image 166
coudy Avatar answered Oct 03 '22 06:10

coudy