Shared object contracts
Define shared request, response, configuration, or domain-object shapes across modules.
TypeScript · Const
An interface names an object type and describes its required or optional members. TypeScript checks interface compatibility structurally, and interfaces can be extended or reopened.
An interface gives a reusable name to an object’s required and optional members. TypeScript checks values structurally, so an object is accepted when it has the required properties with compatible types; interfaces can also be extended, implemented by classes, and reopened for declaration merging.
interface InterfaceName extends BaseInterface {
property: Type;
optionalProperty?: Type;
readonly readOnlyProperty: Type;
}| Name | Type | Description |
|---|---|---|
members | object properties and/or call signatures | The interface declaration itself has no runtime parameters; its members describe the properties and callable signatures expected by the object shape. |
An interface declaration produces a compile-time object type and no runtime value.
interface User {
readonly id: number;
name: string;
email?: string;
}
function greet(user: User): string {
return `Hello, ${user.name}`;
}
const user: User = { id: 1, name: "Ada" };
console.log(greet(user));This runnable example declares an object shape, uses an optional property, prevents reassignment of an identifier, and applies the interface to a function parameter.
Define shared request, response, configuration, or domain-object shapes across modules.
Annotate function parameters and return values with a stable object shape.
Require classes to provide a defined public API.
Extend or augment an existing object shape across declarations.
type aliasextendsimplementsdeclaration mergingstructural typingcall signaturereadonlyoptional properties