Closed
Description
Motivation:
I'm working on a small event simulation library. I'd like to overload the simulate
function so I'm doing the following:
function simulate(element: HTMLElement, name: "mousedown", options: MouseOptions);
function simulate(element: HTMLElement, name: "mousemove", options: MouseOptions);
function simulate(element: HTMLElement, name: "mouseup", options: MouseOptions);
function simulate(element: HTMLElement, name: "mouseover", options: MouseOptions);
function simulate(element: HTMLElement, name: "mouseout", options: MouseOptions);
function simulate(element: HTMLElement, name: "mouseenter", options: MouseOptions);
function simulate(element: HTMLElement, name: "mouseleave", options: MouseOptions);
...
function simulate(element: HTMLElement, name: "keydown", options: KeyboardOptions);
function simulate(element: HTMLElement, name: "keydown", options: KeyboardOptions);
function simulate(element: HTMLElement, name: "keydown", options: KeyboardOptions);
...
Suggestion:
It would be nice if we could allow regex literals or const regexes to be used in the place of string literals when overloading functions as shown below:
const mouseEvents = /mouse(down|move|up|over|out|enter|leave)/;
function simulate(element: HTMLElement, name: mouseEvents, options: MouseOptions);
function simulate(element: HTMLElement, name: /key(up|down|press)/, options: KeyboardOptions);
Since there is no "const" yet maybe we could start with regex literals. The nice thing about allowing constants is that you could then use the same constant in the code in a switch statement or if-else block.