typescript 重复标识符“LibraryManagedAttributes”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52399839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:58:37  来源:igfitidea点击:

Duplicate identifier 'LibraryManagedAttributes'

reactjstypescript

提问by Spenhouet

I have the same issue as in:

我有同样的问题:

React typescript (2312,14): Duplicate identifier 'LibraryManagedAttributes'

反应打字稿(2312,14):重复标识符“LibraryManagedAttributes”

and

TypeScript error: Duplicate identifier 'LibraryManagedAttributes'

打字稿错误:重复标识符“LibraryManagedAttributes”

But I just can't find any solution.

但我就是找不到任何解决方案。

I already upgraded to the latest node/npm/yarn/typescript versions. Also tried downgrading. Nothing helps.

我已经升级到最新的 node/npm/yarn/typescript 版本。也试过降级。没有任何帮助。

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 [email protected] 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.

--verbosesomehow doesn't give me more information.

--verbose不知何故没有给我更多信息。

As I can see LibraryManagedAttributesis defined in:

正如我所见LibraryManagedAttributes,定义在:

  • 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
  • ....
  • 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
  • ....

Where is this coming from? How can I avoid that?

这是从哪里来的?我怎样才能避免这种情况?

I want to find out where this error is coming from so that I can report it to the right entity but I don't know where to start.

我想找出此错误的来源,以便我可以将其报告给正确的实体,但我不知道从哪里开始。

What else can I try?

我还能尝试什么?

回答by Sander Schutten

This seems te happen because Yarn resolves multiple versions of a package; @types/reactin this particular case. Yarn resolves @types/reactfrom your package.json and as a dependency of @types/react-dom.

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

Take the following snippet from my package.json:

从我的package.json 中获取以下片段:

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

The yarn.lockthat is created after you run yarn installcontains something similar to this:

运行后创建的yarn.lockyarn 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@*":
  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"

Notice that @types/react-domdepends on any version of @types/reactas indicated by "*". Yarn resolves two versions of @types/react: "16.4.14"and "15.6.19". This results in the type conflicts you mentioned.

请注意,这@types/react-dom取决于@types/react指示的任何版本"*"。Yarn 解析@types/react:"16.4.14"和 的两个版本"15.6.19"。这会导致您提到的类型冲突。

The solution is to add a resolutions fieldto your package.jsonto tell Yarn to resolve a specific version of @types/react. Take the following sample:

解决方案是在package.json 中添加一个resolutions 字段来告诉 Yarn 解析特定版本的. 取以下示例:@types/react

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

Run yarn installagain. Notice the change in the yarn.lockfile:

再跑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@*", "@types/react@^15.0.16":
  version "15.6.19"
  resolved "https://registry.yarnpkg.com/@types/react/-/react-15.6.19.tgz#a5de18afe65b0f29767328836b48c498a5d3a91b"

Yarn now resolves the same version "15.6.19"for both "@types/react@*"and "@types/react@^15.0.16"dependencies.

Yarn 现在"15.6.19"为两者"@types/react@*""@types/react@^15.0.16"依赖项解析相同的版本。

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.

我想知道自己为什么需要这样做。我希望 Yarn 能够理解它可以解决依赖关系"@types/react" "*""@types/react@^15.0.16"而不是使用最新版本的@types/react.

回答by Spenhouet

This seems to be a typescript issue.

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

My current workaround is adding "skipLibCheck": trueto tsconfig.json.

我目前的解决方法是添加"skipLibCheck": truetsconfig.json.

I want to stress that that is only a workaround and not a fix to the problem it self.

我想强调的是,这只是一种解决方法,而不是解决问题本身。

回答by chosenjuan

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

我得到了同样的错误。我设法通过删除我的“@types/react”然后再次安装它们来修复它。

yarn remove @types/react
yarn add @types/react

回答by mongkuen

For me I had react types duplicated in react-redux, react, and react-intlwhen I upgraded react-intl. The least intrusive fix that's worked for me so far is to run this:

对我来说,我不得不作出反应,重复的类型react-reduxreact以及react-intl当我升级react-intl。到目前为止对我有用的最少侵入性修复是运行这个:

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

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

If the resulting diff of the lockfile looks correct, go ahead and delete node_modules, then yarnto get fresh packages off the deduplicated lockfile.

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

回答by Richard Torcato

The easiest way to fix this for me was to delete my node_modules directory and yarn.lock/package-lock files and then do a yarn install to reinstall all the node modules.

解决这个问题的最简单方法是删除我的 node_modules 目录和 yarn.lock/package-lock 文件,然后执行 yarn install 以重新安装所有节点模块。

回答by tsai

I have the same issue after yarn upgrade @types/react-router-dom. git diffshows multiple versions of @types/reactresolved. In my case, yarn upgrade @types/reactresolves the issue. Removing yarn.lockshould help.

之后我有同样的问题yarn upgrade @types/react-router-domgit diff显示已@types/react解决的多个版本。在我的情况下,yarn upgrade @types/react解决了这个问题。删除yarn.lock应该有帮助。

It seems a fresh (without yarn.lock) install would resolve packages to a consistent state, but a partial upgrade would not resolve the dependencies globally. Thus manual tweaks may be necessary to upgrade all involved packages.

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

回答by Shan Plourde

Related to the question, running npm list @types/reactfrom the directory of your package.json should list duplicate type definitions found in your project.

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

回答by Thiemo Müller

I had a conflicting version request for react in another module I used. Fixing that and re-installing with yarn didn't help me either.

我在我使用的另一个模块中有一个冲突的版本请求。解决这个问题并用纱线重新安装也没有帮助我。

Using NPM instead of Yarn however solved it for me.

但是使用 NPM 而不是 Yarn 为我解决了这个问题。

Hope this helps someone.

希望这可以帮助某人。

回答by trusktr

What worked for me was deleting reactand @types/reactfrom package.json, then in zsh:

对我有用的是删除react@types/reactfrom package.json,然后在 zsh 中:

rm -rf node_modules/**/react
npm i react @types/react

回答by Honza P.

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

In my case I needed to solve the problem manually (using principle described in TS2300). The problem arose once I added ReactKendo to my project.

在我的情况下,我需要手动解决问题(使用TS2300 中描述的原理)。一旦我将 ReactKendo 添加到我的项目中,问题就出现了。

  1. Went to ClientAppdirectory in my project ClientApp\node_modules\@types
  2. Backed up the reactdirectory and then deleted it
  3. Clean + Build + Run project and no more above error occures
  4. I recovered the reactfolder after the bug disappeared and error seems to be gone forever, so it seems to me like typical magic bug somewhere in the universe :-)
  1. 转到ClientApp我项目中的目录ClientApp\node_modules\@types
  2. 备份react目录然后删除
  3. Clean + Build + Run 项目,不再出现上述错误
  4. react在错误消失后我恢复了文件夹并且错误似乎永远消失了,所以在我看来它就像宇宙中某个地方的典型魔法错误:-)

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

我不需要更改配置文件中的任何其他内容。