Skip to content

Move to new file produces syntax errors with overloaded functions #55658

Open
@nberlette

Description

@nberlette

🔎 Search Terms

"move to new file", "new file syntax error", "to new file error", "move to file overloads"

🕗 Version & Regression Information

  • I first noticed this on a nightly build of 5.1.6.
  • It is still occurring on the latest nightly build, 5.3.0-dev.20230905
  • I can't say I ever encountered this bug when using this feature pre-5.0
  • I can't provide a reproduction in the playground because this depends on the mounted file system code actions, which are not available in Monaco

⏯ Playground Link

No response

💻 Code

export function CanBeHeldWeakly<V>(v: V): v is WeakKey<V>;
export function CanBeHeldWeakly<V extends symbol>(v: V): v is V;
export function CanBeHeldWeakly<V extends object>(v: V): v is V;
export function CanBeHeldWeakly(v: unknown): v is object | symbol;
export function CanBeHeldWeakly(v: unknown): v is object | symbol {
  foo();
  return (v != null && (typeof v === "object" || typeof v === "function")) || (
    typeof v === "symbol" && Symbol.keyFor(v) === undefined
  );
}

export function foo(): "bar";
export function foo(bar: "bar"): "bar";
export function foo(bar?: "bar"): "bar" {
  return bar() === "foo" ? "bar" : dependent();
}

export function bar(): "foo" {
  return "foo";
  CanBeHeldWeakly(1);
}

export function dependent() {
  if (CanBeHeldWeakly(globalThis)) {
    return foo();
  }
}

🙁 Actual behavior

If I select the CanBeHeldWeakly function and apply the code action to move it to a new file, the automatic import generated for the foo function on which it depends produces a syntax error. It should only import one identifier - foo - from the file ./test1.ts. As you can see in the screen recording below, it instead imports foo three times (one for each of its overload signatures).

As an experiment, I then moved foo to a new file, and the issue seemed to fix itself in that instance.

It did not occur when I tried only moving foo to another file and not CanBeHeldWeakly.

Screen.Recording.2023-09-06.at.3.48.42.PM.mov

🙂 Expected behavior

I'd expect it to generate a single import for foo and all of its overloads.

Additional information about the issue

No response

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Refactoringse.g. extract to constant or function, rename symbolFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions