我在 React 中遇到了一个奇怪的问题,即运行 npm start
或 npm build
后 TypeScript 没有显示任何错误。下面是我的代码示例,其中我为函数参数和变量分配了无效值。我对这段代码没有任何错误,并且编译得很好,即使它应该失败。我最近将 react 脚本从 v3 升级到 v4,这是我能想到的唯一可能导致此问题的原因。我混合使用了 JS 和 TS。
export default function myFunction(
value: Function = 100,
) {
const test: string = 123
}
我确实收到警告,但没有错误:
Line 31:9: 'test' is assigned a value but never used @typescript-eslint/no-unused-vars
package.json 中的包版本
"react-scripts": "^4.0.3",
"typescript": "^4.2.3",
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "src",
"noFallthroughCasesInSwitch": true
},
"include": [
"src"
]
}
有人有见解吗?
最佳答案
不知何故,TypeScript 在我的开发过程中又开始工作了。奇怪的是,在前几次编译后它仍然无法工作,然后突然开始工作。 ́_(ツ)_/́
我最近做的事情是
- 删除node_modules并重新安装
- 删除我的 tsconfig,然后让 CRA/TypeScript 生成一个新的
我的 TypeScript 版本也降级了。我不确定是我这么做了还是 npm 这么做了。
"react-scripts": "^4.0.3",
"typescript": "^4.1.3",
这是我的新 tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "src"
},
"include": [
"src"
]
}
关于reactjs - React Typescript 没有给出类型检查错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66977125/