如何在*.test.*文件中禁用所有类型检查(包括导入的组件)

huangapple go评论95阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2020年1月3日 13:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定