gpt4 book ai didi

typescript - 当测试/** 未包含在 tsconfig 中时,Vscode TS 语言功能不可用

转载 作者:行者123 更新时间:2023-12-03 20:26:07 25 4
gpt4 key购买 nike

我希望我的 typescript 测试在 test 时接收 linting、代码完成、vscode 智能感知(ts 语言功能)文件夹与 src 相邻.我不希望在构建 typescript 项目时转换测试。

我的 typescript 节点项目的结构如下:

.    
├── server
│ │
│ │
│ ├── src
│ │ │
│ │ └── app.ts
│ ├── test
│ │ │
│ │ └── app.test.ts
│ ├── node_modules/
│ ├── package-lock.json
│ ├── package.json
│ ├── tslint.json
│ └── tsconfig.json
├── front_end/
└── README.md

使用我的 tsconfig:
{
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": true,
"strict": true,
"target": "ESNext",
"paths": {
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"]
}

当我导航到 vscode 中的测试文件时,vscode 语言功能无法识别 node_modules 中安装的类型和包。令人惊讶的是,如果我只在服务器文件夹中打开 vscode(从我的文件夹结构中可以看出服务器不是根目录),语言功能可以工作🧐。

测试使用 ts-jest运行所以 tsc这里不需要。扩展 tsconfig.json 会是更好的做法吗?为了考试🤔?

这是可以在设置中修复的东西还是我应该向 https://github.com/microsoft/vscode/issues/new/choose 提交错误? .

最佳答案

vscode 有一个单根 tsconfig 文件的问题。与 test 相邻的 src 文件夹将使用 tsconfig(在此 pull request 中添加),但是如果您打开的文件夹是它上面的一个目录(例如,如果您有服务器和客户端文件夹),它将不起作用。正在跟踪此问题的解决方案: https://github.com/Microsoft/vscode/issues/12463
此外,如果您想升级到 eslint(不推荐使用 tslint),如果您希望将 test 文件夹排除在主 tsconfig.txt 中,则 typescript 解析器还会强制您将 tsconfig 文件添加到 test 文件夹中。 Link to eslint multi tsconfig docs
简单的解决方案是使用两个额外的 tsconfig 。一个用于 linter tsconfig.eslint.json,另一个用于 test 文件夹中的测试 :test/tsconfig.json 具有以下内容:

{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"include": ["./**/*"]
}
不优雅,但目前不存在更好的解决方案。

关于typescript - 当测试/** 未包含在 tsconfig 中时,Vscode TS 语言功能不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com