Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@bterlson

Description

@bterlson
Issue body actions

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

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.