【问题标题】:TypeScript and React Native, hundreds of errors from node_modules/@types when compilingTypeScript 和 React Native,编译时来自 node_modules/@types 的数百个错误
【发布时间】:2019-06-05 17:59:50
【问题描述】:

我目前正在将我的 RN 项目迁移到 TypeScript。我已经完成了我的大部分 JS 代码,现在当我运行 tsc 时,我遇到了数百个似乎都是错误的错误

error TS 2717: Subsequent property declarations must have the same type. Property
  prop must be of the type TYPE,
  but here has type TYPE

例如:

error TS2717: Subsequent property declarations must have the same type.  Property
  'view' must be of type 'SVGProps<SVGViewElement>',
  but here has type 'SVGProps<SVGViewElement>'.

这是双重混淆,因为列出的类型几乎总是相同的。

我能够运行我的应用程序,我仍然从 tslint 收到有用的消息,并且任何特定于我的代码的错误都会出现在编译器错误列表的底​​部。

我的tsconfig.json 目前是:

{
  "compilerOptions": {
    "allowJs": true,
    "target": "es2018",
    "outDir": "dist",
    "module": "commonjs",
    "sourceMap": true,
    "lib": ["esnext", "es7", "dom", "es6"],
    "jsx": "react-native",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "types": ["react-native"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

我尝试了各种解决方案,包括根据 TypeScript 文档将types 设置为[],并将exclude 设置为各种形式的node_modules,例如./node_modulesnode_modules/* 等,但这些都不是更改产生了任何影响。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    好的,我能够解决这个问题。我不完全确定如何通过 yarn 管理依赖项,但我的问题肯定是依赖项。

    首先,我从依赖项中删除了所有 @types/* 库,然后删除了我的节点模块文件夹。

    然后,尽管大多数教程和指南会说,我只安装了@types/react-native 而不是@types/react@types/react-native 自己添加了反应类型。我在想@types/react 的类型可能取决于节点类型,通过显式安装它们,我在@types/react-native@types/node 之间以及@types/react-native@types/react 之间引入了一堆冲突。我的项目现在编译没有错误。

    【讨论】:

      【解决方案2】:

      问题的根本原因似乎是 Typescript 编译器检查了库。将skipLibCheck: true 添加到tsconfig.json 会跳过检查库。

      详细解释见How to force tsc to ignore node_modules folder?

      【讨论】:

        猜你喜欢
        • 2017-02-06
        • 2019-05-16
        • 2019-03-26
        • 2017-12-23
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        • 2020-10-30
        相关资源
        最近更新 更多