【发布时间】:2022-01-01 11:25:15
【问题描述】:
我有一个jsconfig.json,我将其替换为tsconfig.json。替换后,VSCode 不拾取路径别名,报导入错误:
例子:
Cannot find module '@Components/Title' or its corresponding type declarations.ts(2307)
jsconfig.json
// https://code.visualstudio.com/docs/languages/jsconfig
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@Components/*": ["./src/components/*"],
"@Modules/*": ["./src/modules/*"],
"@Styles/*": ["./src/styles/*"],
"@Content/*": ["./src/content/*"],
"@Apps/*": ["./src/apps/*"]
}
},
//Add any build/compiled folders here to stop vscode searching those
"exclude": ["node_modules", "build"]
}
tsconfig.json
// https://code.visualstudio.com/docs/languages/jsconfig
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@Components/*": ["./src/components/*"],
"@Modules/*": ["./src/modules/*"],
"@Styles/*": ["./src/styles/*"],
"@Content/*": ["./src/content/*"],
"@Apps/*": ["./src/apps/*"]
},
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": ["node_modules", "build"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
更新 1:我只对以 .js 结尾的文件有此问题。如果我将它们重命名为 .ts 一切正常。奇怪的是,如果我将文件重命名为 .js 并重新启动 VSCode,错误就不会再出现在文件中了。
【问题讨论】:
-
通常当我遇到这种情况时,关闭+重新打开代码会为我做这件事;你试过吗?
-
是的,很多次。
-
您是否尝试过从路径中删除前导
./? -
@coppereyecat 试了一下。它没有用。
标签: typescript visual-studio-code tsconfig vscode-jsconfig