interface

A named set of member signatures that a type promises to implement, with no state of its own.

In more detail

Code that depends on an interface can be given any implementation, including a test double, which makes components easy to swap. Some languages, such as Java and C#, require a type to declare the interfaces it implements, while others, such as Go and TypeScript, match them by shape. In TypeScript, interfaces exist only at type-checking time and are removed before the code runs.

Examples

C#: interface names start with I by convention, as in IProductRepository.
In the browser: In TypeScript an interface such as interface BoxSpec { contentWidth: number } lets the compiler check that objects have the right fields. Web specifications also use the word: EventTarget and Performance are interfaces that browser objects implement.
interface User { id: string; name: string }
type Reporter interface {
	Report() string
}
Go: type Reporter interface { Report() string }

Related terms

Taught in

References