Types and contracts
What is structural typing?
On this page
A structural type system checks compatibility using the members a type provides. In TypeScript a value with the right public id: string member can satisfy a corresponding interface without declaring it. Private and protected class members have additional compatibility rules, and excess-property checks can flag fresh object literals.
Worked example
TypeScriptinterface HasId { id: string }
function pathFor(resource: HasId): string {
return "/items/" + encodeURIComponent(resource.id);
}
const invoice = { id: "A/B", amount: 50 };
if (pathFor(invoice) !== "/items/A%2FB") throw Error("Wrong path");Explore class relationships in the OOP Playground