Interfaces and structural typing
suggest changeTypeScript 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents