Skip to content

Union types not working with old-style overloads #1805

Closed
@nycdotnet

Description

@nycdotnet

I was just working with Big.js and tried to pass a union type into its constructor function. It seems that union types don't play nice with old-style overloaded constructors. Please consider the code below that errors with TypeScript 1.4. I would have expected this to work.

interface BigJS_Constructors {
    new (value: number): BigJS;
    new (value: string): BigJS;
    new (value: BigJS): BigJS;
}

interface BigJS extends BigJS_Constructors {
    abs(): BigJS;
    /* more... */
}

var Big : BigJS;

class MyThing {
    public value : BigJS;
    constructor(value: number | string | BigJS) {
        this.value = new Big(value); //This is an error in 1.4.
    }
}

If I change the new signature under BigJS_Constructors to new (value: number | string | BigJS) : BigJS;, and eliminate the other two options, it works fine, but to me the two styles should be identical in this case.

My apologies if I'm doing something stupid here. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    CanonicalThis issue contains a lengthy and complete description of a particular problem, solution, or designDeclinedThe issue was declined as something which matches the TypeScript visionRevisitAn issue worth coming back toSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions