#Intellisense not working for path alias resolution in imports (both VSCode and VS 2022)

8 messages · Page 1 of 1 (latest)

craggy cosmos
#

hello, I have a strange problem with both VSCode and VS 2022:
TSC compiles the output correctly but both the IDEs give me an intellisense error in module resolution and with different error codes.

With this tsconfig.json

"module": "AMD",
"moduleResolution": "Node",
"paths": { "@app-root/*": [ "./app/*" ] },

This import, located in the file app/XXX/file.ts

import { SERVER_MODULE } from "@app-root/server.module";

TSC (5.4.4) Compiles (correctly) to:

define("XXX/file", ["require", "exports", "server.module"], function (...

But VsCode show the error:
Cannot find module '@app-root/server.module' or its corresponding type declarations.ts(2307)

And Visual Studio 2022 (17.11.5) shows a different error:
TS2792 (TS) Cannot find module '@app-root/server.module'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

If importing with the relative path, no error is shown in the IDE and the output is the same

import { SERVER_MODULE } from "../server.module";

Does anybody have a clue about what I'm doing wrong?

slow igloo
#

make sure your tsconfig is in your project root folder (the one you opened in your editor)

#

that's where VSCode picks it up

#

whereas with tsc it depends on your cwd, and it can also walk up the tree until it finds a valid tsconfig

craggy cosmos
#

With tsc im passing -p, the file is in the root dir, alongside the package config

slow igloo
#

is it because your tsconfig isn't named "tsconfig.json"?

craggy cosmos
#

It is named like that, vscode even shows a different file icon for it

craggy cosmos
#

ok, since you didn't mention that anything was blatantly wrong, I tried to reproduce the same situation in a clean repository and maybe I got the root cause.
The tsconfig.json is used as a base file, I have an additional tsconfig.XXX.json that inherits from that:

{
  "extends": "./tsconfig.json",
  "files": [ "./app/XXX/file.ts" ],
  "compilerOptions": { "outFile": "bundle.js" }
}

This is the file that i pass in TSC with the --project flag.

The problem lies in the fact that the main tsconfig has an additional setting that I did not mention before:

"include": [
  "app/*.d.ts",
  "typings/*.d.ts",
  "app/helpers/TsxHelper.ts"
]

This prevents the aliases from working when using directly the base file but, apparently, when adding the files option, the aliasis work for TSC.

Is this a bug or an intended behaviour? I'm using the "include" block to prevent any import outside of my own code and to add the TsxHelper file which has a custom JSX factory