Closed
Description
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).
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
mhegazy commentedon Oct 20, 2017
Duplicate of #12400
undefined
parameters as optional #12400mhegazy commentedon 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.