structural typing
A type rule where two types are compatible if they have the same shape, whatever they are called.
In more detail
In TypeScript an object is accepted wherever its properties match what is required, even if it was never declared as that type. The informal name duck typing comes from the saying that if it walks like a duck and quacks like a duck, it is a duck. This makes test data and composition easy, but two unrelated types with the same fields are interchangeable.
Related terms
- nominal typing — A type system in which two types are compatible only if one is declared to inherit or implement the other, not merely because they have the same members.
- assignable — The rule that decides whether a value of one type may be used where another type is expected.
- excess property checking — An extra TypeScript check that rejects unknown properties when an object literal is written directly where a type is expected.
- interface (TypeScript) — In TypeScript, a named description of an object's shape: which properties it has and their types.
Taught in
- Why TypeScript: Contracts, Erased Types And Soundness — Production TypeScript