Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change VSCode quick fix generated imports for TypeScript to use relative paths?

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?

like image 226
eXavier Avatar asked Nov 02 '17 10:11

eXavier


People also ask

How do I optimize imports in VSCode?

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.

How do you use absolute path in TypeScript?

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).


1 Answers

In your vscode settings, add the following setting:

"typescript.preferences.importModuleSpecifier": "relative"
like image 123
Charlie Sale Avatar answered Nov 15 '22 08:11

Charlie Sale