Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell VS Code to always use relative paths for TypeScript auto imports?

VS Code is auto-importing everything relative to baseUrl using Node-like non-relative paths, which is what I don't want.

How do I tell VS Code to import everything with relative paths (except for Node modules of course)?

Removing the baseUrl option is not an option because I need it in order to point fs imports at a local polyfill of the fs module.

My tsconfig.json has this:

        "baseUrl": "./",
        "paths": {
            "fs/*": [ "./src/util/FileSystem/*" ]
        }

If there's no other way other than removing the baseUrl option, then that doesn't do any good!

like image 535
trusktr Avatar asked Feb 13 '20 04:02

trusktr


People also ask

What is the use of paths in typescript?

TSConfig paths. A series of entries which re-map imports to lookup locations relative to the baseUrl, there is a larger coverage of paths in the handbook. paths lets you declare how TypeScript should resolve an import in your require/imports. {" compilerOptions ":

How to enforce relative path auto imports in VS Code?

To enforce relative path auto imports in VS Code, you can change the importModuleSpecifier setting to value "relative". { "typescript.preferences.importModuleSpecifier": "relative" // ... } The value "non-relative" would create absolute paths based on baseUrl, "auto" ( default setting) selects the shortest path automatically.

Why can’t I use typescript with JavaScript?

There is a problem with this: Compiling the TypeScript code to JavaScript makes it unusable by JavaScript. JavaScript does not understand those module paths. Also, tools like ts-node or ts-jest do not understand these module path imports.

How do I import a module in typescript?

TypeScript by default supports module import. Unlike with those ugly dot slashes, you can configure module paths in tsconfig.json and then use them for import. tsconfig.json.


1 Answers

To enforce relative path auto imports in VS Code, you can change the importModuleSpecifier setting to value "relative".

settings.json (workspace or user):

{
  "typescript.preferences.importModuleSpecifier": "relative"
  // ...
}

The value "non-relative" would create absolute paths based on baseUrl, "auto" (default setting) selects the shortest path automatically.

For JavaScript there is an analogue setting "javascript.preferences.importModuleSpecifier".

like image 147
ford04 Avatar answered Oct 03 '22 03:10

ford04