Say I have 2 interfaces:
interface Interface1{}
interface Interface2{}
is there a way to declare a property as implementing both interfaces? Something like:
class MyClass{
public p: Interface1, Interface2
}
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.
What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .
Similar to languages like Java and C#, interfaces in TypeScript can be implemented with a Class.
An interface type cannot be passed as a parameter. When running TypeScript code, you are really compiling it down to JavaScript and then running the JavaScript. An interface is a TypeScript compile-time construct, so at runtime, there is no such thing as an interface type to call functions on or inspect properties of.
is there a way to declare a property as implementing both interfaces? Something like:
Yup. An intersection type
:
interface Interface1{}
interface Interface2{}
class MyClass{
public p: Interface1 & Interface2
}
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