Skip to content
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

"importModuleSpecifierEnding" not added on "Move to new file" refactoring #46290

Open
jaens opened this issue Oct 10, 2021 · 6 comments
Open
Labels
Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Milestone

Comments

@jaens
Copy link

jaens commented Oct 10, 2021

Bug Report

πŸ”Ž Search Terms

.js extension missing; ECMAScript modules; ESNext

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Not possible, needs refactoring functionality.

πŸ’» Code

Open TypeScript project in VSCode with the following in settings.json:

{ "typescript.preferences.importModuleSpecifierEnding": "js" }

tsconfig.json:

{
    "compilerOptions": {
        "module": "ESNext"
    }
}

Create a file main.ts:

function foo() { }
function bar() { foo(); }

Execute "Move to new file" refactoring on the foo function.

πŸ™ Actual behavior

The following import is generated:

import { foo } from "./foo";

πŸ™‚ Expected behavior

import { foo } from "./foo.js";
@jaens
Copy link
Author

jaens commented Oct 10, 2021

Also, I'm not sure why importModuleSpecifierEnding is only supported in the VSCode settings?
Seems like something that should be in tsconfig.json, or even automatic (based on other settings), considering that AFAIK it's always needed when using new-style ES modules in Node.js.

@andrewbranch
Copy link
Member

It is automatically applied in node12 and nodenext module resolution modes.

@andrewbranch andrewbranch added Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Oct 12, 2021
@andrewbranch andrewbranch added this to the Backlog milestone Oct 12, 2021
@jaens
Copy link
Author

jaens commented Oct 12, 2021

It is automatically applied in node12 and nodenext module resolution modes.

I assume this means "module": "ESNext" with "moduleResolution": "node"?

Indeed, for auto-imports that seems to automatically add a .js ending without needing importModuleSpecifierEnding - though this fact seems somewhat undocumented, none of the ESM examples in the docs even contain a .js extension in the imports, so when the refactoring did not work, I thought I had to add importModuleSpecifierEnding.

@andrewbranch
Copy link
Member

I assume this means "module": "ESNext" with "moduleResolution": "node"?

No, I mean "moduleResolution": "node12" and "moduleResolution": "nodenext".

@jaens
Copy link
Author

jaens commented Oct 13, 2021

No, I mean "moduleResolution": "node12" and "moduleResolution": "nodenext".

Ah, OK, I did not think that was an option since it's not valid according to the tsconfig schema:
image
It is mentioned in the docs.

Though trying that out on 4.5.0-dev.20211013 seems to break loading type declarations for eg. rxjs, so I couldn't use it anyway... (works perfectly with moduleResolution: "node" with ES modules)

error TS7016: Could not find a declaration file for module 'rxjs'. '/home/.../node_modules/.pnpm/[email protected]/node_modules/rxjs/dist/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/rxjs` if it exists or add a new declaration (.d.ts) file containing `declare module 'rxjs';`

3 import { Subject } from "rxjs";
                          ~~~~~~

@andrewbranch
Copy link
Member

Yeah, rxjs will either need to update or we’ll need to decide to do something about it per #46281.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants