Description
TypeScript Version: 2.2.0-dev.20161129
Code
As of the TS version stated above the NodeSelector
in lib.es6.d.ts interface is defined as:
interface NodeSelector {
querySelector<K extends keyof ElementTagNameMap>(selectors: K): ElementTagNameMap[K] | null;
querySelector(selectors: string): Element | null;
querySelectorAll<K extends keyof ElementListTagNameMap>(selectors: K): ElementListTagNameMap[K];
querySelectorAll(selectors: string): NodeListOf<Element>;
}
Expected behavior:
It should be possible to use a generic to control the return type of the general string-based selection methods, rather than being returned a fixed return type of Element
. I.e. assume one uses a class my-svg-shape
which one exclusively applies to either an SVG rect
or circle
.
There should be method signatures to control the return type at the developer's discretion, i.e.
querySelector<E extends Element>(selectors: string): E | null;
querySelectorAll<E extends Element>(selectors: string): NodeListOf<E>;
These would obviously fall back to the current behavior, if used without specifying a type, but could be used as, e.g.
let x = document.querySelectorAll<SVGRectElement | SVGCircleElement>('.my-svg-shape');
Actual behavior:
Return type is fixed to Element
EDIT: Fixed copy-paste typo in application example. Inserted .my-svg-shape
as selector string.