Skip to content

Overloads should consider optional parameters and a type union including undefined as identical #17644

Closed
@bterlson

Description

@bterlson

TypeScript Version: 2.4.2

Code

class A {
  constructor(); // Error: overload signature not compatible with impl
  constructor(a: number | undefined) {}
}

class A {
  constructor();
  constructor(a?: number) {} // this one works
}

Expected behavior:
I expected the first example to work especially since the type of parameter a is identical in either constructor implementation. I think it is helpful when writing overloads to not use optional parameters in the implementation signature and be explicit about the types that might exist in each position.

Actual behavior:
The first results in the noted error.

Commentary:
The following error may a moral equivalent of the error I am objecting to:

function foo(a: number | undefined) { }
foo(); // error, even though it's "implicitly" passing undefined

but I think this case is different as on the call side the error is justifiable on the grounds that we think it's likely the caller forgot a parameter (even if undefined is an allowable type, you should be explicit).

Activity

added
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
SuggestionAn idea for TypeScript
on Aug 7, 2017
mhegazy

mhegazy commented on Oct 20, 2017

@mhegazy
Contributor

Duplicate of #12400

added
DuplicateAn existing issue was already created
and removed
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
SuggestionAn idea for TypeScript
on Oct 20, 2017
mhegazy

mhegazy commented on Nov 6, 2017

@mhegazy
Contributor

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

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bterlson@DanielRosenwasser@mhegazy

        Issue actions

          Overloads should consider optional parameters and a type union including undefined as identical · Issue #17644 · microsoft/TypeScript