首页 > 解决方案 > VSCode 在 TypeScript 中不显示缺少导入

问题描述

我刚刚在一个新文件夹中创建了一个新的 TypeScript 项目。TypeScript 是 4.4.2 版。

文件夹结构如下

- myProject
  -src
   -folder-1
     -file-1.ts
   -folder-2
     -file-2.ts
  -tsconfig.json

包含逻辑的文件非常简单

文件 1.ts

function function_1() {
    console.log('Print something');
}

文件 2.ts

function function_2() {
    function_1();
}

我习惯看到的是 VSCode 在唯一的函数行中显示红色错误,function_2因为function_1没有导入(甚至没有导出)。

相反,VSCode 没有显示任何错误。与file-2.tsCTRL+Click行相反,VSCode 打开file-1的实际实现。function-1()function-1

更多,如果我运行tsc编译器不会抱怨并在文件夹中创建相关的 * .js文件dist。自然,如果我尝试执行function-2(),我会收到一条错误消息,告诉我"function_1 is not defined"

这是我的tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "dist",
        "sourceMap": true,
        "allowJs": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "resolveJsonModule": true
    },
    "include": ["src/*.ts", "src/**/*.ts"],
    "exclude": ["node_modules"],
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2016", "dom"]
}

还有一个细节。如果我运行命令npm run tsc src/folder-2/file-2.tstsc运行 TypeScript 编译器的标准 npm 脚本在哪里),我会在控制台上收到一个错误,但编译器仍然会生成file-2.js作为其输出。

我很确定我错过了一些愚蠢的东西,但我看不到它。

标签: typescriptvisual-studio-code

解决方案


推荐阅读