Interfaces and structural typing

suggest change

TypeScript is structurally typed: compatibility depends on members, not on explicit declarations. Interfaces are well suited to extendable object contracts; type aliases can additionally describe unions, primitives, tuples, and mapped types.

interface Named {
  readonly name: string;
}

interface Employee extends Named {
  department: string;
  describe(): string;
}

class Developer implements Employee {
  constructor(
    readonly name: string,
    public department: string,
  ) {}

  describe(): string {
    return `${this.name} works in ${this.department}`;
  }
}

const person: Named = new Developer("Ada", "Platform");
console.log(person.name);

Structural compatibility allows a value to have more members than the target type requires. Fresh object literals receive an additional excess-property check, which catches many misspelled property names but is not exact-object typing.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents