Helping Jetbrains IDE to set correct absolute paths for TS imports
In the project I work on, we use the following ESLint rule to prevent relative parent imports:
1 | // @typescript-eslint/parser doesn't work well with import/no-relative-parent-imports |
It works well, but I was getting annoyed by the fact that auto-imports added by PhpStorm still used relative paths. Even worse, the quick-fix action Convert path to absolute was changing the path to a wrong one. For example, instead of js/example I was getting .//assets/js/example. I did two things to fix it:
- Marked
resources/assetsas Resource Root. It’ll depend on your directory structure. - Went to Preferences > Editor > Code Style > TypeScript > Imports and checked
Use paths relative to tsconfig.json.
Now the auto-imports use absolute paths! And if ever need to use a quick-fix (hopefully not), then I get a new one – Change ES6 import path, which works as expected.