Typescript does not resolve modules through tsconfig.json's baseUrl

My tsconfig.json:

{
"compilerOptions": {
"module": "es6",
"target": "es6",
"lib": ["dom", "es2016"],
"moduleResolution": "node",
"baseUrl": "src",
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"sourceMap": true,
"jsx": "preserve"
},
"files": [
"typings/Core.d.ts"
]
}

 

My project structure:

- node_modules/
- scripts/
- src/
- components/
- services/
- typings/
- www/
- tsconfig.json

 

Whenever I create a component and try to import a service like this:

import { logout } from 'services/authentication/index';

I get an error: 

TS2307: Cannot find module 'services/authentication/index'

Even though simply compiling the Typescript through the CLI works fine. 

 

8

I still have pretty much the same issue on PhpStorm v2024.3 …

My tsconfig:

{
  "compilerOptions": {
    "paths": {
      "@ctrl/*": ["src/controller/*"],
      "@ui/*": ["src/ui/*"],
      "@/*": ["src/*"],
      "*": ["src/@custom_types/elements/*"]
    },
    "typeRoots": [
      "src/@custom_types/libraries",
      "node_modules/@types"
    ],
    // <…>
  },
  // "files": []
}

With this, mostly works as expected, but tsc --build generates files in wrong locations.

If I uncomment the "files": [] part, then build works as expected, but all these are red with “Cannot find module" errors:

import custom from "custom";
import var1 from "@ui/vars";
import var2 from "@ctrl/vars";

Restarting IDE doesn't help

0

As the "files": [] array is empty, no files are included in this config, so path mappings defined in it are not applied when resolving modules.

0

But that's just IDE behaviour I believe. Everything else works as expected. Crtl + Click also works. TSC build also works. Webpack build also works… It's only that IDE marks all files, that have such imports, red

And I believe empty files: [] should be just fine, because behaviour changes if I define files or remove this option averall.

I want all files to be generated into /build, so I can't really list all the files. If I remove files: [] completely, then .d.ts files are generated right next to .ts files and polutes the src code.

Also, I'm using a monorepo, so I don't even want .d.ts files generated in root, but only in packages. If I don't have files: [], tsc --build also generates files in root next to each TS and TSX.

No matter how I look at it, it seems as an IDE issue. If it was a TS issue, wouldn't it throw an error on build?

0

When building, ts files are piped through loaders and not passed to the Typescript compiler directly, and the compiler configuration is specified explicitly, whereas the built-in compiler service uses the nearest tsconfig.*.json file the current *.ts file is included in, traversing the folders tree up to the project root. No files are included in your config because of the empty "files": [] array, so it's not used for any files processing.

0

OK, how do I disable this inspection on IDE/project level, because @ts-ignore is not an option in this case. I don't care that IDE can't find the module which is there

0

The errors come from the Typescript compiler service, the only way to suppress compiler errors is using @ts-ignore.

0

So what you're saying, is that Jetbrains IDEs don't allow a proper use of TS with additional compiler packages, which deal with such situations?

0

请先登录再写评论。