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

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.

主站蜘蛛池模板: 龙泉市| 阆中市| 北川| 河池市| 武川县| 津市市| 泰州市| 贵阳市| 磐石市| 苍溪县| 平山县| 宁城县| 武川县| 大石桥市| 汉寿县| 永平县| 邯郸市| 壶关县| 岳阳市| 武邑县| 缙云县| 涡阳县| 开封市| 罗山县| 嘉善县| 临海市| 旌德县| 砀山县| 武陟县| 井陉县| 筠连县| 宜宾县| 仙居县| 两当县| 皮山县| 比如县| 托克托县| 称多县| 奉新县| 阿图什市| 樟树市|