reactjs - React Typescript 没有给出类型检查错误

标签 reactjs typescript

我在 React 中遇到了一个奇怪的问题,即运行 npm startnpm 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/

相关文章:

javascript - Nest 无法解析装饰器内包装的防护的依赖关系

javascript - Flex 与 HTML5/TypeScript 之间的通信

node.js - 仅使用 gulp 或 webpack 构建此 Node 项目

reactjs - Meteor 导入 SVG 作为 React 的组件

javascript - 命名函数是 jsx Prop 的好习惯吗?

reactjs - 如何将 chart.js 与 React Hook 一起使用?

reactjs - 如何在 React JS 中应用全屏限制?

reactjs - React、Flux、状态和 Store

javascript - 路由不适用于捆绑的(webpack)Angular 2 项目

angular - RxJS observable 在 mergeMap 后不触发订阅