Specialized overloading signature

We can use a specialized signature to create multiple methods with the same name and number of parameters, but a different return type. To create a specialized signature, we must indicate the type of function parameter using a string. The string literal is used to identify which of the function overloads is invoked:

interface Document { 
    createElement(tagName: "div"): HTMLDivElement; // specialized 
    createElement(tagName: "span"): HTMLSpanElement; // specialized 
    createElement(tagName: "canvas"): HTMLCanvasElement; // specialized 
    createElement(tagName: string): HTMLElement; // non-specialized 
} 

In the preceding example, we have declared three specialized overloaded signatures and one nonspecialized signature ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.