TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

isolatedDeclarations fixer inserts unnecessary inline imports

Open benjaminjkraft opened this issue 1 year ago • 0 comments

🔎 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).

benjaminjkraft avatar Oct 18 '24 00:10 benjaminjkraft