Browse OOP reference
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

TypeScript
interface 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");

TypeScript Handbook on compatibility

Explore class relationships in the OOP Playground

How do modern languages approach object orientation?What is a method?What is method overloading?

All OOP questions

Search ObjectFAQ