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

Suggestion: interface to allow overloading variables (similar to unions in C lang) #2958

Closed
sandeepkalra opened this issue Apr 29, 2015 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@sandeepkalra
Copy link

Hi,

I have code that I want to rewrite in typescript, and for that I want to do few things that looks like this:

interface msg {
    x: number;
    Run: ()=>string;
    Run: (number) => string;
}

interface msg {
    Code: number;
    Message: string;
    Message: Object;
}

and many more similar things. Typescript has concept of 'overload' for functions. This is almost in same lines and more probably in line with "C" 'unions' style.

Is it possible to accept this as a feature request if this is not present and/or proposed yet for Typescript?

Thanks,

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 29, 2015
@RyanCavanaugh
Copy link
Member

This has been implemented as Union types, see #805 for an overview. Basically, you can write var x: string|number to mean "x is a string or a number"

@danquirk
Copy link
Member

interface msg {
    x: number;
    Run: { 
        (): string;
        (number): string 
    }
}

interface msg {
    Code: number;
    Message: string|Object;
}

But note that string|Object likely doesn't do what you want because everything is an Object.

@sandeepkalra
Copy link
Author

Thanks.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants