Skip to content

isolatedDeclarations fixer inserts unnecessary inline imports #60266

@benjaminjkraft

Description

@benjaminjkraft

🔎 Search Terms

isolatedDeclarations autofix fix imports

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about isolated declarations. (It seems to go back to the original commits that created the infer-return-type fixers.)

⏯ Playground Link

https://github.com/benjaminjkraft/typescript-autofix-import-bug

💻 Code

// callee.ts
export interface T { myField: string }
export function f(): T {
	return { myField: "hello" }
}

// index.ts
import { f } from "./callee"
export function g() {
	return { wrapper: f() }
}

🙁 Actual behavior

Isolated declarations reports an error for g() (correct). But the fix is to add : { wrapper: import("./callee").T }, which is correct but needlessly unreadable.

🙂 Expected behavior

Instead, the fix should add : { wrapper: T }, and then an appropriate import.

Additional information about the issue

I think the fix may just be to remove the if here? That seems to work in my local testing, although I haven't tested very exhaustively.

I suspect this applies in a few other places (e.g. other calls into the same code with the same pattern), but I didn't look very closely. It also applies to a number of other patterns where you return something exported from elsewhere; this was just the most convenient example to explain.

I may be able to contribute a fix for this (at least once my company reviews the CLA).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions