TypeScript 尝试使用 Yarn Workspaces 编译兄弟文件夹的 node_modules

标签 typescript yarnpkg yarn-workspaces

我正在尝试使用 TypeScript 和 Yarn Workspaces 创建一个包含“服务器”和“客户端”文件夹的 monorepo 项目。在没有创建“client”文件夹的情况下编译“server”文件夹但效果很好,但是当我创建“client”文件夹时,编译“server”文件夹使得 TypeScript 想要编译“client”的 node_modules 文件夹,但出现错误尽管如此编译。

错误如下:

../../../../.config/yarn/global/node_modules/typescript/lib/lib.dom.d.ts:5353:11 - error TS2300: Duplicate identifier 'FormData'.

5353 interface FormData { ~~~~~~~~

../node_modules/@types/react-native/globals.d.ts:40:15 40 declare class FormData { ~~~~~~~~ 'FormData' was also declared here.

../../../../.config/yarn/global/node_modules/typescript/lib/lib.dom.d.ts:5363:13 - error TS2300: Duplicate identifier 'FormData'.

5363 declare var FormData: { ~~~~~~~~

../node_modules/@types/react-native/globals.d.ts:40:15 40 declare class FormData { ~~~~~~~~ 'FormData' was also declared here.

../../../../.config/yarn/global/node_modules/typescript/lib/lib.dom.d.ts:15671:11 - error TS2300: Duplicate identifier 'URLSearchParams'.

15671 interface URLSearchParams { ~~~~~~~~~~~~~~~

../node_modules/@types/react-native/globals.d.ts:258:15 258 declare class URLSearchParams { ~~~~~~~~~~~~~~~ 'URLSearchParams' was also declared here.

我试过:

  • 不使用工作区,
  • 将“客户端”指定为“服务器”的依赖项,
  • 从“服务器”tsconfig 中排除 node_modules,

但没有结果。

我创建了一个测试仓库来重现错误(1- 克隆仓库,2- yarn 安装,3- cd 服务器,4- tsc):https://github.com/lewislbr/typescript-test

我错过了什么?

提前致谢。

最佳答案

通过以下方式解决:

  • 将“@types/node”安装为常见的开发依赖项,
  • 在公共(public) tsconfig 中添加 "types": ["node"]。

🎉

关于TypeScript 尝试使用 Yarn Workspaces 编译兄弟文件夹的 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57608871/

相关文章:

typescript - Monorepo中的后端 typescript :如何获取Docker的依赖关系?

node.js - yarn - 在不同的文件夹中安装包的依赖项?

javascript - 我可以将 NPX 与 yarn 一起使用吗?

visual-studio - 尝试对 "$(lastknowntypescriptversion) that evaluates to "进行数字比较“而不是数字

javascript - 从其他组件访问激活的路由数据

package.json - 删除重复包以及 yarn install --pure-lockfile 和 yarn install freeze-lockfile 之间的区别

node.js - package.json~ 和 package.json.orig 的用途是什么?

typescript - 使用 yarn 2 工作区在 monorepo 中的两个项目之间共享 typescript 代码

node.js - ngOninit 没有被称为 Angular 2 Express

Angular4 Material 输入显示不正确