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

Path completions for Node.js subpath imports #52460

Closed
5 tasks done
thetutlage opened this issue Jan 27, 2023 · 9 comments · Fixed by #57718
Closed
5 tasks done

Path completions for Node.js subpath imports #52460

thetutlage opened this issue Jan 27, 2023 · 9 comments · Fixed by #57718
Labels
Domain: Completion Lists The issue relates to showing completion lists in an editor Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@thetutlage
Copy link

Suggestion

Node.js for a while has a way to define import aliases (known as subpath imports) in package.json file. The subpath imports also work fine when I compile my code using tsc and inside my code editor (VSCode).

However, as I type the import path in my code editor, no suggestions related to subpath imports are shown.

Given, I have the following subpath import alias inside package.json.

{
  "imports": {
    "#controllers/*": "./app/controllers/*.js"
  }
}

I should be able to type #controllers and get suggestions. Infact, using an export symbol from one of the controller files should write the auto-import using the alias.

tsconfig.json

Following is a trimmed down version of my tsconfig.json file.

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "lib": ["ESNext"],
    "rootDir": "./",
    "outDir": "./build",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "types": ["@types/node"]
  },
  "include": ["./**/*"],
  "exclude": ["./node_modules", "./build"]
}

🔍 Search Terms

✅ Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Since, TypeScript can understand and compile subpath imports, it will be great, if they are also suggested during import autocomplete

📃 Motivating Example

Have nice experience of auto-importing files with aliases.

💻 Use Cases

Right now, I have to either manually type the complete import statement by hand. Or type the export symbol name and update the import path to use the subpath alias.

Attaching a small screen recording of the same

subpath-imports.mov
@xobotyi
Copy link

xobotyi commented Dec 20, 2023

Same here, except the IDE used.

Problem is somehow fixable with paths cofiguration wthin tsconfig.json, but it becomes a problem for cases when some of used packages uses same import alias.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jan 30, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 30, 2024
@mbrevda
Copy link

mbrevda commented Feb 14, 2024

Looks like this was fixed in #55015

@RomainLanz
Copy link

Is there a version we can try to ensure it works as expected?

In the meantime, we are duplicating it inside the paths key of our tsconfig.

@mbrevda
Copy link

mbrevda commented Feb 14, 2024

npm i typescript@next

@DanielRosenwasser
Copy link
Member

I believe #55015 adds auto-imports, but not path completions within the string literals.

@tonivj5
Copy link

tonivj5 commented Aug 31, 2024

I can confirm in 5.6.1-rc helps with auto-imports when there's already an import.

  1. With subpath import already in file:
    image
  2. Without any reference in file:
    image

And, as @DanielRosenwasser has mentioned, there isn't any completions in import string path
image

@custardcream98
Copy link

For now, it seems that adding the following to your tsconfig.json file does the trick:

{
  "compilerOptions": {
    "paths": {
      "#*":["./src/*"],
    }
  }
}

@DanielRosenwasser
Copy link
Member

@tonivj5 what compilerOptions do you have?

@tonivj5
Copy link

tonivj5 commented Sep 6, 2024

@DanielRosenwasser here it's

  "compilerOptions": {
    "baseUrl": ".",
    "rootDir": ".",

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,

    "target": "es2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "moduleDetection": "force",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "verbatimModuleSyntax": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "lib": ["es2022", "ESNext.Disposable", "dom", "es2023.array"],

    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,

    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "importHelpers": true,
    "pretty": true,

    "sourceMap": true,
    "composite": true,
    "declaration": true,
    "declarationMap": true,
    "incremental": true,

    "emitDeclarationOnly": false,
    "noEmit": false,
    "noEmitOnError": true,

    "tsBuildInfoFile": "./dist/tsconfig.app.tsbuildinfo"
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Completion Lists The issue relates to showing completion lists in an editor Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants