PathIntellisense
PathIntellisense copied to clipboard
tsconfig baseUrl behaviour
Project folder structure
<Root>
tsconfig.json
/src
/managers
/managerA.ts
/managerB.ts
tsconfig.js
{
"compilerOptions": {
"sourceRoot": "./",
"baseUrl": "./"
}
}
If I want to import managerA in managerB.ts, using an absolute path, this works in typescript 2.3
import managerA = require("src/managers/managerA")
However path-intellisense does not recognise this unless I add this mapping for path-intellisense
{
"src": "{workspaceRoot}/src"
}
path-intellisense seems to think typescript works by starting the filepath with "./" to alias to the baseUrl, but this is not how it seems to work in typescript 2.3. So when I want to use a relative path path-intellisense does not recognise I want the path relative to the current file.
import managerA = require("./managerA")
^ this should be recognized by path-intellisense, but as soon as I type "./" it puts me in the project root.
Also when I type "./" it lists the current folders/files in the root, but it fails to continue the intellisense when I try to go deeper.
@Gregroam Thank you for very well explained issue :-)
What I do at the moment is create a mapping entry for the base url.
In your case it would look like this:
{
"./": "{workspaceRoot}/./"
}
Which is why it './' resolves to your workspaceRoot.
This is the logic at the moment:
mappings.push({ key: baseUrl, value:
${workspace.rootPath}/${baseUrl} })
I guess it would make more sense like this:
mappings.push({ key: baseUrl, value:
${tsconfig-path}/${baseUrl} })
Since many projects have the tsconfig in the root, I started with this case. There are numerous cases for which I don't thought about how to handle them best. For example whats with the nested tsconfigs?
I am going to change the mapping to use the tsconfig path instead. What do you think? Is there anything I should consider before doing this?
Perhaps a way to disable the automatic mapping through the vscode settings when baseUrl
is used, so we can define our own custom mapping in case the automatic mapping doesn't work how we want it to?
how to use "jsconfig.json" or "tsconfig.json" mappings folder