英文:
How to disable ALL type checking in *.test.* files (including imported components)
问题
"exclude": ["*.test.ts", "*.test.tsx"]
在 tsconfig 中只会阻止对测试特定类型(例如 describe、it、expect 等)的类型检查。但我在 vscode 的每个测试文件中仍然看到导入组件的错误。唯一禁用这些导入组件错误的方法是在每个测试文件的开头使用 //@ts-nocheck
。
是否有一种方法可以从 tsconfig 中禁用所有测试文件的所有类型检查,包括来自非测试文件的导入组件?
完整的 tsconfig.json
文件(Create React App 项目):
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src"],
"exclude": ["*.test.ts", "*.test.tsx"]
}
英文:
"exclude": ["*.test.ts", "*.test.tsx"]
in tsconfig only prevents type checking of the test specific types (e.g. describe, it, expect, etc). I'm still seeing errors for imported Components within every test file in vscode. The only way to disable these imported component errors is with //@ts-nocheck
at the start of every test file.
Is there a way to disable ALL type checking for all test files from the tsconfig, including any imported components from non-test files?
full tsconfig.json
file (Create React App project)
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src"],
"exclude": ["*.test.ts", "*.test.tsx"]
}
</details>
# 答案1
**得分**: 1
我最终选择了在这个其他问题中列出的按文件禁用选项。
https://stackoverflow.com/questions/54506744/how-can-i-disable-all-typescript-type-checking
如果你的测试都在一个文件夹中,你可以尝试排除那个文件夹,或者按照该帖子中列出的方法禁用 .js 文件扩展名。
<details>
<summary>英文:</summary>
I ended out going with the disable per file option listed in this other question
https://stackoverflow.com/questions/54506744/how-can-i-disable-all-typescript-type-checking
If your tests are all in 1 folder you could try to exclude that folder, or go with disabling the .js file extension also listed in that thread.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论