Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kevinbarabash opened this issue Dec 2, 2014 · 5 comments
Labels
Suggestion An idea for TypeScript

Comments

@kevinbarabash
Copy link

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.

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Dec 2, 2014
@danquirk
Copy link
Member

danquirk commented Dec 2, 2014

I'm not sure how well this would play with the resulting signature help for these overloads compared to the explicit version that you have to write today.

I'm not following what you mean with the last sentence about using the same constant in the implementation.

@DanielRosenwasser
Copy link
Member

You might also be interested in #1003 instead.

@kevinbarabash
Copy link
Author

@DanielRosenwasser If you could define a type as being "click" | "dblclick" | "mousedown" | "mousemove" | ... that would definitely cut down on the number of overloaded definitions.

@danquirk Some code will probably explain things better:

const mouseRegex = /mouse(down|move|up|over|out|enter|leave)/;
const keyboardRegex = /key(up|down|press)/;

function simulate(element: HTMLElement, name: mouseRegex, options: MouseOptions);
function simulate(element: HTMLElement, name: keyboardRegex, options: KeyboardOptions);
function simulate(element: HTMLElement, name: string, options: Options);
function simulate(element: HTMLElement, name: string, options: Options) {
    if (mouseRegex.test(name)) {
        // create a mouse event
    } else if (keyboardRegex.test(name)) {
        // create a keyboard event
    }
}

The thought was that the regex could be by both the type system and also by the code.

@kevinbarabash
Copy link
Author

Closing because it's pretty much covered by #1003 and #1007.

@fletchsod-developer
Copy link

When ES7 specification come into force, you will find it include the support for overloading.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants