我在构建步骤中在node_module
中得到多个React.js类型错误:
TS2320: Interface 'Element' cannot simultaneously extend types 'ReactElement<any>' and 'ReactElement<any>'. Named property 'type' of types 'ReactElement<any>' and 'ReactElement<any>' are not identical.
个S2320: Interface 'ElementClass' cannot simultaneously extend types 'Component<any, {}, any>' and 'Component<any, {}, any>'. Named property 'props' of types 'Component<any, {}, any>' and 'Component<any, {}, any>' are not identical.
个TS2300: Duplicate identifier 'LibraryManagedAttributes'.
个TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>', but here has type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>'.
个
一开始我以为这个问题与未修复的软件包版本有关,所以我找到了一个使用相同软件包的工作项目,并编辑了我的package.json
文件以使用确切的版本。-没有帮助。
然后我让我的同事在他的机器上做了这件事,它确实工作了!所以我调整了node
,npm
和yarn
以匹配相同的版本。-没有帮助。
在那之后,我完全删除了node
,npm
,yarn
并重新安装它们。* 尽管它也没有解决这个问题。*
我目前的设置是:
- 节点v10.9.0
- npm v6.2.0
- Yarnv1.9.4
package.json
:
"devDependencies": {
"@babel/core": "^7.0.0-rc.1",
"@babel/plugin-transform-typescript": "^7.0.0-rc.1",
"@babel/preset-env": "^7.0.0-rc.1",
"@babel/preset-react": "^7.0.0-rc.1",
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.1",
"@types/react": "^16.4.12",
"@types/react-dom": "^16.0.7",
"@types/react-router-dom": "^4.3.0",
"awesome-typescript-loader": "^5.2.0",
"babel-loader": "^8.0.0-beta",
"commitizen": "^2.10.1",
"commitlint": "^7.0.0",
"css-loader": "^1.0.0",
"cz-customizable": "^5.2.0",
"extract-text-webpack-plugin": "4.0.0-beta.0",
"html-webpack-plugin": "^3.2.0",
"husky": "^0.14.3",
"node-sass": "^4.9.3",
"sass-loader": "^7.1.0",
"source-map-loader": "^0.2.4",
"standard-version": "^4.4.0",
"style-loader": "^0.22.1",
"typescript": "^3.0.1",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"@material-ui/core": "^1.5.1",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-router-dom": "^4.3.1"
}
字符串
tsconfig.json
:
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "esnext",
"jsx": "react"
},
"include": [
"./src/**/*"
]
}
型
错误列表:
ERROR in [at-loader] ./node_modules/@types/react/index.d.ts:2305:19
TS2320: Interface 'Element' cannot simultaneously extend types 'ReactElement<any>' and 'ReactElement<any>'.
Named property 'type' of types 'ReactElement<any>' and 'ReactElement<any>' are not identical.
ERROR in [at-loader] ./node_modules/@types/react/index.d.ts:2306:19
TS2320: Interface 'ElementClass' cannot simultaneously extend types 'Component<any, {}, any>' and 'Component<any, {}, any>'.
Named property 'props' of types 'Component<any, {}, any>' and 'Component<any, {}, any>' are not identical.
ERROR in [at-loader] ./node_modules/@types/react/index.d.ts:2312:14
TS2300: Duplicate identifier 'LibraryManagedAttributes'.
ERROR in [at-loader] ../../../node_modules/@types/react/index.d.ts:2326:13
TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>', but here has type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>'.
型
2条答案
按热度按时间q43xntqr1#
前一段时间我也遇到了同样的问题,它与
./node_modules/@types
无关。使用nvm
- node版本管理器和node
,npm
,yarn
及其全局包的完全清除解决了我的问题。卸载Node.js和NPM需要相当多的a lot of steps,这就是为什么我建议使用自动化解决方案,如
node-reinstall script
。字符串
这将删除所有包,并完全删除Node.js和NPM,并将其替换为Node Version Manager。如果安装成功,您将能够看到如下内容:
型
如果您使用Homebrew安装
yarn
,则只需执行brew uninstall yarn
,否则yarn
已从系统中删除。现在你将不得不照顾
yarn
packages.cd
到~/.config/yarn/global
,并删除node_modules
,package.json
和yarn.lock
。就这样,所有的东西都被删除了,你可以用
nvm
安装Node和NPM了。要这样做,请输入:型
它将下载、编译和安装最新版本的Node和NPM。
现在检查所有内容:
型
y3bcpkx12#
当
yarn
安装不同版本的@types/react
时,我遇到了这个问题。我安装了17.0.2
版本,但某些依赖项也需要它,并为它们安装了更新的版本。我发现的修复程序(不确定它是否正确!)是添加字符串
在我的root
package.json
(“root”,因为我使用的是root)中。从那以后,所有的版本都变得一致了。
参考:https://yarnpkg.com/configuration/manifest#resolutions
我使用的是
yarn
版本4.0.2
。编辑我发现了另一个更简单,可能更好的解决方案。
我所要做的就是把它添加到我的
tsconfig.json
中:型
这使得
tsc
不包括monorepo根目录中所有“hilled”包的所有类型。参考:https://www.typescriptlang.org/tsconfig#types