In this section, we will continue from the last segment and delve further in TypeScript. In this section, we will look how to use interfaces with TypeScript. If you are coming from system’s language background say C# or JAVA, you must be knowing different OOPs terminology and Interfaces are one of the key concepts for enforcing constraints on implementing classes. Let us go ahead and jump at demos.
In the above example, I have first created one interface, which basically accepts one input as number and prints the output as number. Next, I have a class which implements this interface. Fairly simple interface. With the above change in place, when I run the app, it will produce the following output.
Now, let me also explain, if I try to change the signature while calling the same, how intellisense flags the error to me.
This is the beauty of TypeScript, It’s giving me potential errors while writing the code itself, which off course save lots of time debugging the issue. Now, let us go ahead and look few complex examples. You can see in the below snippet, I am getting very nice intellisense from TypeScript, which is offcourse nice to avoid any mistake.
Let me put the finished snippet here.
With the above change in place, when I run the app, this will produce the following output.
I hope you would have liked today’s discussion. Thanks for Joining me. We will delve further in the coming posts.Till then, stay tuned and Happy Coding.