Skip to content

allow regex literals and const regexes/strings to be used when overloading string constants #1328

Closed
@kevinbarabash

Description

@kevinbarabash

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions