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

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

Closed
bterlson opened this issue Aug 7, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@bterlson
Copy link
Member

bterlson commented Aug 7, 2017

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).

@DanielRosenwasser DanielRosenwasser added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Aug 7, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 20, 2017

Duplicate of #12400

@mhegazy mhegazy marked this as a duplicate of #12400 Oct 20, 2017
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Oct 20, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

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

@mhegazy mhegazy closed this as completed Nov 6, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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