Interface
In object-oriented programming, an interface describes the set of properties and methods that an object exposes. An object that implements an interface can be handled correctly when passed to functions or constructs designed to interoperate with such interfaces. Usually, an interface is treated as a contract: other than the behavior of these properties and methods, objects implementing the same interface may not be related in any way at runtime.
The JavaScript language itself defines some interfaces, such as iterable, thenable, and disposable, which interoperate with language constructs such as spreading, await, and using. Due to the lack of compilation or static type checking, these contracts are not embodied by any language constructs, so MDN also refers to them as "protocols".
In TypeScript, the interface declaration creates interfaces whose implementation can be enforced at compile-time.
In web standards terminology (WebIDL in particular), an interface describes the shape of objects provided by Web APIs. Unlike the OOP sense, interfaces in WebIDL are actually embodied in the form of a constructor function and a prototype object. For example, the HTMLButtonElement interface is exposed as the HTMLButtonElement constructor and the HTMLButtonElement.prototype object (which all instances inherit from). This makes WebIDL interfaces behave more similarly to JavaScript classes. An interface can inherit from other interfaces, and can include members from mixins.
See also
- Using classes
- WebIDL
- Mixin
- Information contained in a WebIDL file
- Interface (object-oriented programming) on Wikipedia
- WebIDL specification