重复标识符‘ LibraryManagedAttritribute’

我也有同样的问题:

反应类型脚本(2312,14) : 重复的标识符 & # 39;

还有

类型脚本错误: 重复标识符 & # 39; 库管理属性 & # 39;

但我就是找不到解决办法。

我已经升级到最新的节点/npm/纱线/打印脚本版本。也试过降级。没有用。

yarn build --verbose
yarn run v1.9.4
$ react-scripts-ts build --verbose
Creating an optimized production build...
Starting type checking and linting service...
Using 1 worker with 2048MB memory limit
ts-loader: Using typescript@3.0.3 and C:\dev\project\frontend\tsconfig.prod.json
Warning: member-ordering - Bad member kind: public-before-private
Failed to compile.


C:/dev/project/frontend/node_modules/@types/prop-types/node_modules/@types/react/index.d.ts
(2312,14): Duplicate identifier 'LibraryManagedAttributes'.




error Command failed with exit code 1.

--verbose无法给我更多信息。

As I can see LibraryManagedAttributes is defined in:

  • node_modules/@types/react/index.d.ts
  • node_modules/@types/prop-types/node_modules/@types/react/index.d.ts
  • node_modules/@types/react-overlays/node_modules/@types/react/index.d.ts
  • ....

这是怎么回事? 我怎么才能避免这种情况?

我想找出这个错误来自哪里,这样我就可以报告给正确的实体,但我不知道从哪里开始。

我还能试什么?

57643 次浏览

这似乎是一个打字稿的问题。

我目前的工作方法是将 "skipLibCheck": true添加到 tsconfig.json

我想强调的是,这只是一个变通办法,而不是解决问题本身。

这似乎是因为 Yarn 解析了一个包的多个版本; 在这种特殊情况下是 @types/react。纱线从 package.json 解析 @types/react,并作为 @types/react-dom的依赖项。

从我的 包裹 Json中摘录以下片段:

"devDependencies": {
"@types/react": "^15.0.16",
"@types/react-dom": "^0.14.23"
...
}

在运行 yarn install之后创建的 yarn.lock包含类似的内容:

"@types/react-dom@^0.14.23":
version "0.14.23"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-0.14.23.tgz#cecfcfad754b4c2765fe5d29b81b301889ad6c2e"
dependencies:
"@types/react" "*"


"@types/react@*":
version "16.4.14"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.4.14.tgz#47c604c8e46ed674bbdf4aabf82b34b9041c6a04"
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"


"@types/react@^15.0.16":
version "15.6.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"

请注意,@types/react-dom依赖于 "*"所指示的任何版本的 @types/react。纱线解决了 @types/react的两个版本: "16.4.14""15.6.19"。这将导致您提到的类型冲突。

解决方案是在 包裹 Json中添加一个 分辨率字段,告诉纱线解析特定版本的 @types/react。以下面的例子为例:

"resolutions": {
"@types/react": "^15.0.16"
}

再次运行 yarn install。请注意 纱,锁文件中的更改:

"@types/react-dom@^0.14.23":
version "0.14.23"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-0.14.23.tgz#cecfcfad754b4c2765fe5d29b81b301889ad6c2e"
dependencies:
"@types/react" "*"


"@types/react@*", "@types/react@^15.0.16":
version "15.6.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"

纱线现在为 "@types/react@*""@types/react@^15.0.16"依赖解析相同的 "15.6.19"版本。

I would like to know myself why this is needed. I would expect Yarn to understand it can resolve dependency "@types/react" "*" with "@types/react@^15.0.16" instead of resolving it with the latest version of @types/react.

对我来说,解决这个问题的最简单方法是删除 node _ module 目录和 yarn.lock/package-lock 文件,然后进行一次纱线安装,以重新安装所有的节点模块。

我有同样的问题后 yarn upgrade @types/react-router-domgit diff显示已解析的 @types/react的多个版本。在我的例子中,yarn upgrade @types/react解决了这个问题。移除 yarn.lock应该会有帮助。

新安装(没有 yarn.lock)似乎可以将软件包解析为一致的状态,但是部分升级不能解析全局的依赖关系。因此,手动调整可能是必要的,以升级所有涉及的软件包。

在我的例子中,我得到的错误表明‘ LibraryManagedAttritribute’是在两个不同的位置声明的。沿着这些路径,我意识到一个已安装的模块也有一个 package.json 文件,该文件还添加了“@type/response”作为 dep,其版本与根 package.json 文件中的版本不同。我把这两个改成了同一个版本,问题就解决了。

I got the same error. I managed to fixed it by removing my '@types/react' and then installing them again.

yarn remove @types/react
yarn add --dev @types/react

对我来说,我有反应类型复制在 react-reduxreactreact-intl时,我升级了 react-intl。到目前为止,对我来说最不具侵入性的解决方案就是运行这个:

npx yarn-deduplicate --packages @types/react yarn.lock

如果锁文件的结果差异看起来正确,继续并删除 node_modules,然后 yarn从已删除的锁文件中获取新的包。

与这个问题相关的是,从 package.json 目录运行 npm list @types/react应该列出在项目中找到的重复类型定义。

在我使用的另一个模块中,我有一个相互冲突的版本请求。修复和重新安装纱线也没有帮助我。

Using NPM instead of Yarn however solved it for me.

希望这对谁有帮助。

对我有效的方法是从 package.json中删除 react@types/react,然后在 zsh 中:

rm -rf node_modules/**/react
npm i react @types/react
C:/Users/japa/source/repos/ReactTestApp/TemplateExample/ClientApp/node_modules/@types/react/index.d.ts
TypeScript error in C:/Users/japa/source/repos/ReactTestApp/TemplateExample/ClientApp/node_modules/@types/react/index.d.ts(2835,14):
Duplicate identifier 'LibraryManagedAttributes'.  TS2300

在我的例子中,我需要手动解决这个问题(使用 TS2300中描述的原理)。当我将 ReactKendo 添加到我的项目中时,问题就出现了。

  1. 转到我的项目 ClientApp\node_modules\@types中的 ClientApp目录
  2. 备份 react目录,然后删除它
  3. 清除 + 生成 + 运行项目,不再出现上述错误
  4. 在 bug 消失后,我恢复了 react文件夹,错误似乎永远消失了,所以在我看来,它就像是宇宙中某个地方的典型神奇 bug: -)

I did not need to change anything else in config files.

对我来说,它只引起 @types/react-redux。固定的 npm i --save-dev @types/react,所以 package.json看起来像这样:

  ...
"devDependencies": {
"@types/react": "^16.9.19",
"@types/react-redux": "^7.1.7"
...
}

我在处理链接依赖项时遇到了这个问题。我的链接包住在一个勒纳回购,它有 @types/react作为一个依赖。我添加了 @types/react作为一个对等依赖,切换到 Yalc我的工作流程,并能够继续。

在我们的案例中,我们通过

  1. 将所有 @types/*包转移到 devDependencies

  2. rm -rf yarn.lockrm -rf node_modules

  3. 再次运行 yarn install

使用 纱线去重为我解决了这个问题。

步骤:

  1. 可选安装 yarn-deduplicate软件包或在步骤2中使用 npx
npm install -g yarn-deduplicate

or

yarn global add yarn-deduplicate
  1. 运行 yarn-deduplicate
yarn-deduplicate yarn.lock --packages @types/react yarn.lock

或者

npx yarn-deduplicate --packages @types/react yarn.lock
  1. 删除 node_modules文件夹
rm -rf node_modules
  1. 重新安装依赖项
yarn install

在我的示例中,当 @types/react(v17.0.3)和 @types/react-dom(v17.0.2)的版本号不同步时,出现了错误。

为了解决这个问题,我删除了 @types/react,因为它是从 @types/react-dom悬挂起来的。