liveBook · Manning

Method intersection

As explained in Essential TypeScript 5, Third Edition

A method intersection is a callable method type formed by intersecting the signatures of same-named methods from constituent types.

A method intersection forms a callable method type by intersecting same-named method signatures from constituent types. Its behavior resembles overloads, but the resulting type can be difficult to implement. section 10.3.2

Person and Employee contact methods

Person defines getContact(field: string): string, while Employee defines getContact(field: number): number. Their intersection exposes both call signatures. An implementation accepting string | number can return the matching contact value. section 10.3.2

Inspect the compiler’s result and account for these behaviors:

  • Use the generated declaration file to verify how the compiler merged the signatures. section 10.3.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains signature merging, inferred call results, implementation compatibility, and declaration-file inspection, but warns that consequences can be difficult to determine.
Examples
Uses Person and Employee getContact methods with string and number parameters, logging Alice and 6512346543.
What you can do after
Diagnose compiler-assigned method signatures by inspecting declarations and implement compatible union-parameter behavior.
How it connects
Connects intersection types with function overloading, union types, runtime type checks, and declaration files.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.