简体   繁体   中英

How to make Visual Studio Code use path mappings for automatic imports?

I want to have absolute paths in my project to avoid ugly paths and to have constant imports wherever a file is in the project tree.

I have added this in the tsconfig.json

"paths": {
  "@myapp/*": ["*", "app/*"]
}

I can now have working imports like import {... } from '@myapp/...' but the automatic imports from Visual Studio Code are still relative.

I tried this in the settings.json (as written in https://blog.angulartraining.com/path-mapping-with-typescript-cac16d12c416 )

"editor.codeActionsOnSave": {
    "source.organizeImports": true
}

But it didn't help. How to make Visual Studio Code's automatic imports use the paths mapping. Also, is there a tool that would help me refactor all of my code?

  1. Navigate to File>Preferences>Settings>User Settings on VSCode

  2. Set "typescript.preferences.importModuleSpecifier": "non-relative"

If you have both - mapped paths & relative - after PR #40637 you can use

"typescript.preferences.importModuleSpecifier": "shortest"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM