How to create Interface in JavaScript?

4

I think a lot of you know about the Interface concept, which is very widespread in OOP, but in JavaScript in all the projects I've worked on until today I have not seen this concept. I researched a little bit about, but most posts are old, so I'd like to know if these new JS updates came up somehow similar to those used in other languages to declare interface? If not, what is the proper way to create an interface today?

    
asked by anonymous 18.11.2018 / 00:22

1 answer

3

It's not exactly possible, even in newer versions of the language. It is possible in TypeScript, and a lot of people are going to it , precisely because it is more robust and complete.

What you can do in JS is just to adopt certain patterns. You can create a class with methods without implementation. It is far from ideal, will not do what is more important in the interface that is enforcing a contract, but it is something like that. There are 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.

Other than this 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 compromised with contracts.

    
18.11.2018 / 00:29