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?
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.
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.
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!
Interface is a structure that defines the contract in your application. It defines the syntax for classes to follow.
It's certainly possible, just inline the inner interfaces like this:
interface ComplexSetting {
data: {
simpleData: {enable: boolean;};
view: {expandSpeed: string;};
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With