Skip to content

Isolated declarations inconsistent about banning default assignment patterns for function parameters Β #58549

Closed
@lucacasonato

Description

@lucacasonato

πŸ”Ž Search Terms

  • isolated declarations
  • function parameter
  • default value

πŸ•— 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

export function y(array: number[] = [], comparator: (a: number, b: number) => number): void {};

export const x = {
    y(array: number[] = [], comparator: (a: number, b: number) => number): void {}
}

πŸ™ Actual behavior

For function x and y no diagnostic is raised on the property array at all.

For method z.x, a diagnostic is raised on array regarding the type annotation of array not including undefined. For method z.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 marking comparator as optional, or adding a default value assignment.

This is for multiple reasons:

  1. The diagnostic raised for z.x suggests changing the type annotation to T | 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 be undefined in the body itself, because any undefined value would be replaced with the default argument).
  2. An isolated declarations emitter can not determine whether it is safe to emit this or not, because knowing that requires knowing whether the type of array contains undefined. See

Additional information about the issue

cc @dragomirtitian

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Isolated DeclarationsRelated to the --isolatedDeclarations compiler flagFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions