Skip to content

Add option to disable TS using inline import types in quick fixes #35078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjbvz opened this issue Nov 13, 2019 · 5 comments · Fixed by #36615
Closed

Add option to disable TS using inline import types in quick fixes #35078

mjbvz opened this issue Nov 13, 2019 · 5 comments · Fixed by #36615
Labels
Domain: Quick Fixes Editor-provided fixes, often called code actions. Fix Available A PR has been opened for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Nov 13, 2019

TypeScript Version: 3.8.0-dev.20191112

Search Terms:

  • Implement interface
  • quick fix
  • code action
  • add missing imports

Code
For a small project:

iface.ts

export interface IValue {
    value: number;
}

export interface IFace {
    do(value: IValue): void;
}

index.ts

import { IFace } from "./iface";

export class Foo implements IFace {}
  1. Run implement interface on Foo

Current Behavior:
The generated code is:

import { IFace } from "./iface";

export class Foo implements IFace {
    do(value: import("./iface").IValue): void {
        throw new Error("Method not implemented.");
    }
}

Request
We would like an option to tell TypeScript to not use inline imports and instead always add normal imports at the top of the file.

import { IFace, IValue } from "./iface";

export class Foo implements IFace {
    do(value: IValue): void {
        throw new Error("Method not implemented.");
    }
}

/cc @bpasero Since I know you were passionate about this issue

@bpasero
Copy link
Member

bpasero commented Nov 13, 2019

Duplicate of #34995

@andrewbranch
Copy link
Member

In #32910, @DanielRosenwasser said

We should always prefer an import statement at the top of the file.

In that fix, we didn’t discuss adding an option, we just made it always do an import declaration. @mjbvz do people really want an option, or does it seem like most people just want the import declaration?

It should also be noted that #35200 could have an impact on this discussion, so we may want to wait and see where it lands, particularly on whether the default behavior of the compiler’s elision of imports used only in type positions is going to change.

@andrewbranch andrewbranch added In Discussion Not yet reached consensus Suggestion An idea for TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. labels Dec 10, 2019
@mjbvz
Copy link
Contributor Author

mjbvz commented Dec 10, 2019

@andrewbranch Yes I think VS Code would prefer that imports always be added at the top of the file. I'm not sure if other users prefer inline imports however, which is why I proposed having a setting to control this. We should try getting actual user feedback on this

Also 👍 for #35200

@andrewbranch
Copy link
Member

@mjbvz @DanielRosenwasser: with type-only imports in master, the discussion at #36038, and #36412 looking likely to be merged, what’s the latest feeling on the right thing to do here?

On one hand, auto-imports will not use import type, so maybe we should match that here and use a regular import at the top of the file. On the other hand, that has unwanted effects if importsNotUsedAsValues is preserve or error. Could that be used as a heuristic to write a type-only import? (And actually, should that same heuristic be used for auto-imports?)

@mjbvz
Copy link
Contributor Author

mjbvz commented Jan 25, 2020

For VS Code, we would prefer adding regular imports. I agree though that this should not introduce an error, so observing importsNotUsedAsValues and using import type instead makes sense to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Quick Fixes Editor-provided fixes, often called code actions. Fix Available A PR has been opened for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
3 participants