【问题标题】:Type checking issues with using pnpm and typescript together一起使用 pnpm 和 typescript 的类型检查问题
【发布时间】:2018-08-02 21:30:35
【问题描述】:

据我了解,tutorial pnpm 在node_modules 下创建符号链接的.registry.npmjs.org 和其他条目点。我的项目在typescript 上,我有@types 用于输入node_modules。但是这个@types也有node_modules/.registry.npmjs.org/@types。所以我收到一个错误,例如:

/node_modules/.registry.npmjs.org/@types/jquery/3.3.5/node_modules/@types/jquery/index.d.ts(32,14): error TS2300: Duplicate identifier 'jQuery'.

...和

/node_modules/@types/jquery/index.d.ts(28,14): error TS2300: Duplicate identifier 'jQuery'.

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": [
      "es5",
      "dom",
      "es2015.promise"
    ],
    "experimentalDecorators": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "scripts",
    "src/contracts"
  ]
}

有什么解决办法吗?

【问题讨论】:

  • 您是否已经在您的tsconfig.json 文件中设置了"exclude": ["node_modules/.registry.npmjs.org"]?这是我会尝试的第一件事,但我不会将其作为答案发布,因为我不太相信它会起作用。
  • @MattMcCutchen 是的,我试过了,但没有帮助......我也试过"typeRoots": [ "./node_modules/@types"]
  • 我无法重现该问题。 tsc --traceResolution 可能会有所帮助。如果您无法从输出中发现问题,请将其发布在问题中,我会查看。
  • @AlexFilatov 你使用什么版本的打字稿?我知道 v1 存在符号链接问题,但 v2/3 工作正常。实际上 pnpm 是用 typescript 编写的,而 typescript 2 没有这些问题
  • pnpm 也使用@types,参见package.json

标签: typescript types symlink typechecking pnpm


【解决方案1】:

对我来说"typeRoots": ["./node_modules/@types"] 工作。

默认情况下tsc 将在所有node_modules/@types 文件夹中查找类型。

您可以通过调用tsc --listFiles 来测试包含哪些文件。

我想因为这个文件是由typescript本身包含的,那么它也会包含pnpm repo store node_modules/.pnpm/@types中的所有文件。

xxx/node_modules/.pnpm/typescript@3.9.7/node_modules/typescript/lib/lib.es5.d.ts

就我而言,我有多个版本的 React 正在阅读。

【讨论】:

  • 执行此操作后,我仍然收到错误,因为 typescript 解析了 .pnpm: Type 'import("c:/Users/rajas/Documents/GitHub/meal-planner/node_modules/rc-menu/lib/interface").MenuClickEventHandler' is not assignable to type 'import("C:/Users/rajas/Documents/GitHub/meal-planner/node_modules/.pnpm/rc-menu@9.0.12_react-dom@17.0.2+react@17.0.2/node_modules/rc-menu/lib/interface").MenuClickEventHandler'. 中的文件
猜你喜欢
  • 2020-07-08
  • 2019-03-18
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
相关资源
最近更新 更多