Skip to content

Function overloads for function expressions #25761

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

Closed
OliverJAsh opened this issue Jul 18, 2018 · 3 comments
Closed

Function overloads for function expressions #25761

OliverJAsh opened this issue Jul 18, 2018 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@OliverJAsh
Copy link
Contributor

TypeScript Version: 2.9.2

Search Terms:

Code function overload expression

Related #16731

I have this TypeScript code which uses overloads on a function declaration. This code works as expected.

    function identity(x: string): string;
    function identity(x: number): number;
    function identity(x: string | number): string | number {
        return x;
    }

    const a = identity('foo') // string
    const b = identity(1) // number
    const c = identity({}) // type error (expected)

I am trying to achieve the equivalent of the above using function expressions instead of function declarations, however I get a type error:

    /* Type '(x: string | number) => string | number' is not assignable to type '{ (x: string): string; (x: number): number; }'.
        Type 'string | number' is not assignable to type 'string'.
            Type 'number' is not assignable to type 'string' */
    const identity: {
        (x: string): string;
        (x: number): number;
    } = (x: string | number): string | number => x;

How I can achieve the same effect of overloading the function but with function expressions?

Usage of function declarations vs expressions is a matter of preference. Ideally we would be able to achieve the same behaviour using either techniques.

  • Is this possible?
  • Is the above behaviour a bug?
@mhegazy
Copy link
Contributor

mhegazy commented Jul 18, 2018

Is this possible?

no.

Is the above behavior a bug?

No. this is behaving as intended. there is a stronger relationship between overloads and an implementation signature that allows us to relax some of the rules.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 18, 2018
@OliverJAsh
Copy link
Contributor Author

Interesting, so it's not possible to achieve the same behaviour we see with the function declaration but using the function expression syntax? That's a shame. I guess we'll have to stick to function declarations for functions which require overloads.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants