- Hands-On Functional Programming with TypeScript
- Remo H. Jansen
- 167字
- 2021-07-02 14:03:14
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 non-specialized signature for the function named createElement.
When we declare a specialized signature in an object, it must be assignable to at least one non-specialized signature in the same object. This can be observed in the preceding example, as the createElement property belongs to a type that contains three specialized signatures, all of which are assignable to the non-specialized signature in the type.
When writing overloaded declarations, we must list the non-specialized signature last.
- 一步一步學(xué)Spring Boot 2:微服務(wù)項目實戰(zhàn)
- Building a Home Security System with Raspberry Pi
- Reactive Programming with Swift
- Learn Swift by Building Applications
- 表哥的Access入門:以Excel視角快速學(xué)習(xí)數(shù)據(jù)庫開發(fā)(第2版)
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- Unity UI Cookbook
- OpenGL Data Visualization Cookbook
- Emgu CV Essentials
- 計算機應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- CRYENGINE Game Development Blueprints
- Serverless Web Applications with React and Firebase
- App Inventor 2 Essentials
- Emotional Intelligence for IT Professionals
- Simulation for Data Science with R