How to create Javascript interface?

Asked

Viewed 4,432 times

7

I believe most of you know the concept of Interface, fairly widespread in POO, but in Javascript in all projects I worked until today I have not seen use of this concept. I did some research on, but most of the posts are old, so I wonder if in these new JS updates came up any similar way used in other languages to declare interface? Otherwise, what is the proper way to create an interface today?

1 answer

8


It is not exactly possible, even in newer versions of the language. It is possible in Typescript, and many people are going to her, precisely because it is more robust and complete.

What you can do in JS is just adopt certain standards. You can create a class with methods without implementation. It is far from ideal, it will not do what is most important in the interface that is to enforce a contract, but it is something similar. It has a attempts to do something close, but it’s ugly. There are other ways to do this, but honestly if you really need it it’s time to go to TS.

Apart from that, I can only say that it is not part of the philosophy of language to have this kind of verification. Javascript is a dynamic typing language, flexible, never committed to contracts.

  • Thank you very much for the information, I was with this perception, I did not know if it was lack of knowledge on my part or if really today I did not have such resource in the language. And really the ways I’ve seen get pretty ugly even kk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.