Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode / tslint does not resolve paths in tsconfig.json

vscode is somehow not honoring my tsconfig.json (for a couple of weeks now, it has been different. eiter my bad or vscode update...)

{
"compileOnSave": false,
"compilerOptions": {
    "baseUrl": "./",
    "paths": {
        "@foo-animation/*": [
            "src/app/animation/*"
        ],
        ...

enter image description here

Respectively in the Problems tab:

Cannot find module '@foo-animation/animation-micro' or its corresponding type declarations. ts(2307)

Regular base paths (like @angular/core) are properly resolved, just my ‘custom’ ones are the problem...

Compiling, Building, Running... all works like a charm. So I believe from an angular/typescript-perspective everything is fine. (Also, my fellow developers using IntelliJ have no issues…) So it seems to boild down to „telling vscode about it“.... :-/

My tsconfig.json sits in the root-folder of the project. the only thing is, that I use another tsconfig.app.json, which includes above tsconfig.json.

enter image description here

So is there a way to tell vscode where to look for it's tsconfig.json (to encourage parsing those paths) ?

This SO question and this VSCode github issue might be related, but I still don't know what to do.

like image 435
Frank Nocke Avatar asked Nov 08 '20 11:11

Frank Nocke


2 Answers

I had a similar issue while setting up my repositories recently and I think adding this line to your vscode settings.json should help:

"tslint.configFile": "./tslint.json"

If your tslint.json is not in the root you should add your own path.

like image 105
ziale Avatar answered Oct 22 '22 18:10

ziale


You could use the typeRoots and types compileOptions.

typeRoots by itself tells tsc where to find type definitions. see typeRoots

"typeRoots": [
      "dirPath/@angular",
      "node_modules/@types",
      "node_modules/electron"
]                     /* List of folders to include type definitions from. */

Using types tells it whether there are specific ones that you would like to be resolved. Any other types will be ignored. see types

    "types": [
      "animation-micro",
      "jquery",
      "node",
      "react",
      "prop-types",
      "." //referencing types belonging to paths specified in typeRoots i.e. 'dirPath/@angular/.' , 'node_modules/@types/.' &  'node_modules/electron/.' - needed to resolve namespaces (dependencies)
],                

Note: Specifying typeRoots and types compileOptions will exclude types that are usually included by default, unless explicitly included.

Other helpful alternative, depending on your runtime setup and requirements, could be to use the rootDirs compileOption.

rootDirs allows you to treat everything almost like an 'Über jar' in java (the idea of everything being available in one place). see rootDirs

"rootDirs": [
      "/ts/", 
      "/js/",
      "src/main/resources/static/ts",
      "src/main/resources/static/js"
 ],/* List of root folders whose combined content represents the structure of the project at runtime. */

Depending on your use case, you may not even need both tsconfig files anymore using these.

Also note, using extends overwrites the referenced tsconfig settings, so be careful that you haven't accidentally overwritten some of your paths. That could also potentially cause issues.

like image 1
Zach Avatar answered Oct 22 '22 16:10

Zach