TypeScript 和 React Native,编译时来自 node_modules/@types 的数百个错误

Rob*_*zak 8 typescript

我目前正在将我的 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
Run Code Online (Sandbox Code Playgroud)

例如:

error TS2717: Subsequent property declarations must have the same type.  Property
  'view' must be of type 'SVGProps<SVGViewElement>',
  but here has type 'SVGProps<SVGViewElement>'.
Run Code Online (Sandbox Code Playgroud)

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

我能够运行我的应用程序,我仍然从 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"]
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种解决方案,包括设置types[]按打字稿文档和设置exclude各种形式的node_modules,如./node_modulesnode_modules/*等,但没有这些变化有任何影响。

Rob*_*zak 6

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

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

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


Ssh*_*ack 5

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

请参阅如何强制 tsc 忽略 node_modules 文件夹?详细解释。