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.
请先登录再写评论。
I still have pretty much the same issue on PhpStorm v2024.3 …
My tsconfig:
With this, mostly works as expected, but
tsc --buildgenerates 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:Restarting IDE doesn't help
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.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 removefiles: []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 --buildalso 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?
When building,
tsfiles 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 nearesttsconfig.*.jsonfile the current*.tsfile 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.OK, how do I disable this inspection on IDE/project level, because
@ts-ignoreis not an option in this case. I don't care that IDE can't find the module which is thereThe errors come from the Typescript compiler service, the only way to suppress compiler errors is using
@ts-ignore.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?