Isolated declarations inconsistent about banning default assignment patterns for function parameters #58549
Labels
Bug
A bug in TypeScript
Domain: Isolated Declarations
Related to the --isolatedDeclarations compiler flag
Fix Available
A PR has been opened for this issue
Milestone
π Search Terms
π Version & Regression Information
5.5.0-dev.20240514
β― Playground Link
https://www.typescriptlang.org/play/?target=99&isolatedDeclarations=true&ts=5.5.0-dev.20240514#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVwJ4AUAhlFEXgFxwpIC2ARsFANoC6cAvHGwDRxoRaYEkRjQqxKjQZM+9KXUZQAlJwB81RU2VUAbhEwATOAG8AvgG4AUFdCRY-HAGd4ITqatwv+YqXIKZFnYuXkchETEoCSIApTlY7XVNQJ04fSNTMyszIA
π» Code
π Actual behavior
For function
x
andy
no diagnostic is raised on the propertyarray
at all.For method
z.x
, a diagnostic is raised onarray
regarding the type annotation ofarray
not includingundefined
. For methodz.y
, no diagnostic is raised.π Expected behavior
I would expect that in all four cases to raise an ID diagnostic on
array
, with the only possible fix being markingcomparator
as optional, or adding a default value assignment.This is for multiple reasons:
z.x
suggests changing the type annotation toT | undefined
. This is because without ID, TypeScript would automatically add| undefined
to the emitted declarations. However, this is a bad suggestion, as doing so does not just change the external signature of the function, but also changes the type for the parameter of the function, in the function body, to a new type that does not match with the actual type of the parameter in the function (it can never beundefined
in the body itself, because anyundefined
value would be replaced with the default argument).array
containsundefined
. SeeAdditional information about the issue
cc @dragomirtitian
The text was updated successfully, but these errors were encountered: