官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 桦南县| 新宾| 饶阳县| 浙江省| 蓬莱市| 青川县| 和静县| 肥乡县| 万荣县| 公主岭市| 温州市| 平塘县| 安岳县| 宝清县| 连江县| 贵德县| 湟源县| 呼图壁县| 彭水| 兴安盟| 武安市| 焦作市| 汽车| 新和县| 西青区| 甘孜县| 长汀县| 毕节市| 炉霍县| 云梦县| 南投县| 商水县| 扶绥县| 昌宁县| 西吉县| 西城区| 建阳市| 湖北省| 溆浦县| 昭觉县| 龙门县|