问题描述
我有一个带有 TypeScript 的 Next.js 项目。我的 E2E 测试是用 Cypress 编写的。我需要告诉 tsc
如何处理多个配置。它需要处理不同文件夹的不同配置。
Next.js 在项目根目录中的 tsconfig.json
是这个。
{
"compilerOptions": {
"allowJs": true,"baseUrl": "./src","esModuleInterop": true,"forceConsistentCasingInFileNames": true,"isolatedModules": true,"jsx": "preserve","lib": ["dom","dom.iterable","esnext"],"module": "esnext","moduleResolution": "node","noEmit": true,"resolveJsonModule": true,"skipLibCheck": true,"strictPropertyInitialization": true,"strictnullchecks": true,"strict": true,"target": "es5"
},"exclude": ["node_modules"],"include": ["next-env.d.ts","**/*.ts","**/*.tsx"]
}
Cypress 有自己的 cypress/tsconfig.json
。
{
"extends": "../tsconfig.json","compilerOptions": {
"target": "es5","lib": ["es5","dom"],"isolatedModules": false,"types": ["cypress","@testing-library/cypress"],"noEmit": true
},"include": [
"**/*.ts"
]
}
"type-check": "tsc --pretty --noEmit"
运行此脚本失败,因为它尝试使用项目根目录的配置编译 Cypress 文件夹。如何编写此类型检查命令,以便它为每个文件夹使用正确的 tsconfig.json
?
解决方法
我认为最简单的方法是让 type-check
脚本运行 tsc 两次,一次用于 nextjs 文件,一次用于 cypress 文件:
"type-check": "tsc -p tsconfig.json --pretty --noEmit && tsc -p cypress/tsconfig.json --pretty --noEmit"
您还需要修改 nextjs tsconfig.json
以排除 cypress 文件夹中的文件:
"exclude": ["node_modules","cypress"],
(这在任何情况下都可能是个好主意 - 如果您的 cypress 文件中有错误,您的下一个应用程序不需要编译失败)。