I use VSCode (1.17.2 at time of writing) with TypeScript (2.5.2). When I start to use a class in .ts file that is not yet imported, there is error and the class name i underlined with possibility to use Quick Fix bulp to import it. But it uses absolute path (actually relative to project root) to the file, e.g.
import { FormattedValue } from 'src/app/entities/formattedValue';
It works but when I create modules that can be just copied to different projects (having different project's folder structure) I need the paths to be relative. So when I start using e.g. FormattedValue
in file src/app/entities/xy.ts
, the generated import should look like
import { IFormattedValue } from './formattedValue';
Is there a way how to setup VSCode to use relative paths in quick fix generted imports?
In VSCode, go to File -> Preferences -> Settings and click on the icon in the top right hand corner to open up the settings in JSON. Et voilà! Your imports will now be organized every time you save a file.
To be able to use absolute paths in TypeScript we can set the baseUrl property in the tsconfig. json file. With this, we define src as our root directory (for module resolution).
In your vscode settings, add the following setting:
"typescript.preferences.importModuleSpecifier": "relative"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With