Skip to content

Structural typing — a value fits a type if it has the members, whatever it is called

Level: 201 · for TypeScript users coming from Java, C# or Rust

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: TypeScript compares shapes, not names: a plain object literal passes as a Dog and fake instanceof Dog prints false; a class with a #private field is the exception, since no object literal can have one (TS2741).

Keywords: structural typing, duck typing

What the finished page will answer

  • Why does const fake: Dog = { name, speak } compile, and what does fake instanceof Dog print?
  • Why does a #lives field stop the same trick with TS2741, and does a TypeScript private field do the same?
  • May a value with more properties than the type asks for be assigned, and when is the extra property an error?
  • Are two separately named types with the same members interchangeable in both directions?
  • Does a class have to say implements Dog to be used where a Dog is expected?

Examples it will need

  • structural_typing_fake_dog_sh.sh — a clean tsc check for an object literal used as a Dog, TS2741 for the class with a #private field, then Node printing the fake's bark and false for instanceof
  • structural_typing_same_shape_ts.ts — values of two separately named types with the same members, each passed to a function that expects the other

See also

Sources to start from