Tutorial by Examples

Let's suppose we have a reference to the JQuery type definition and we want to extend it to have additional functions from a plugin we included and which doesn't have an official type definition. We can easily extend it by declaring functions added by plugin in a separate interface declaration with ...
Declare public variables and methods type in the interface to define how other typescript code can interact with it. interface ISampleClassInterface { sampleVariable: string; sampleMethod(): void; optionalVariable?: string; } Here we create a class that implements the interface. ...
Suppose we have an interface: interface IPerson { name: string; age: number; breath(): void; } And we want to create more specific interface that has the same properties of the person, we can do it using the extends keyword: interface IManager extends IPerson { managerId:...
One of the core benefits of Typescript is that it enforces data types of values that you are passing around your code to help prevent mistakes. Let's say you're making a pet dating application. You have this simple function that checks if two pets are compatible with each other... checkCompatible...
Like classes, interfaces can receive polymorphic parameters (aka Generics) too. Declaring Generic Parameters on Interfaces interface IStatus<U> { code: U; } interface IEvents<T> { list: T[]; emit(event: T): void; getAll(): T[]; } Here, you can see that our t...
The primary reason to use interfaces to achieve polymorphism and provide developers to implement on their own way in future by implementing interface's methods. Suppose we have an interface and three classes: interface Connector{ doConnect(): boolean; } This is connector interface. Now we...
TypeScript supports interfaces, but the compiler outputs JavaScript, which doesn't. Therefore, interfaces are effectively lost in the compile step. This is why type checking on interfaces relies on the shape of the object - meaning whether the object supports the fields and functions on the interfac...

Page 1 of 1