提问人:Maxim Gershkovich 提问时间:8/31/2018 最后编辑:Hendy IrawanMaxim Gershkovich 更新时间:11/12/2021 访问量:37013
TypeScript - [后续属性声明必须具有相同的类型] - 对同一类型定义的多个引用
TypeScript - [Subsequent property declarations must have the same type] - Multiple references to the same type definitions
问:
我对使用 TypeScript 和 Webpack 相当陌生,不幸的是,我的类型声明一直遇到我似乎无法解决的问题。
简单地说,我的项目是一个 ASP.NET MVC React 应用程序,它使用 NPM、TypeScript 和 Webpack 来管理 JavaScript 依赖项。我遇到的问题是,虽然我的项目可以成功编译,但我的看起来像这样。180 errors
TS2717 (TS) Subsequent property declarations must have the same type. Property 'webview' must be of type 'DetailedHTMLProps<WebViewHTMLAttributes<HTMLWebViewElement>, HTMLWebViewElement>', but here has type 'DetailedHTMLProps<WebViewHTMLAttributes<HTMLWebViewElement>, HTMLWebViewElement>'.
现在,如果我通过单击类型并按“转到定义”来仔细查看,我可以清楚地看到,我的项目为此处可见的相同类型定义了两个引用:
问题是这两个文件似乎都是我的项目tsconfig.json文件的要求,因为没有它们,它就无法编译。
我的tsconfig.json如下所示:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"lib": [ "es5", "es2017", "dom"]
"removeComments": true,
"typeRoots": [
"/node_modules/@types",
"/Types/"
]
},
"compileOnSave": false,
"exclude": [
"/node_modules/",
"/bin",
"/obj"
]
}
我的package.json文件如下所示:
{
"name": "fungalai",
"version": "1.0.0",
"dependencies": {
"react": "16.4.2",
"react-dom": "16.4.2"
},
"devDependencies": {
"@types/flux": "3.1.7",
"axios": "^0.18.0",
"deep-diff": "^1.0.1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "^6.24.1",
"create-react-class": "^15.6.3",
"expose-loader": "^0.7.5",
"jszip": "^3.1.5",
"flux": "3.1.3",
"ts-loader": "^4.3.0",
"typescript": "3.0.1",
"uglifyjs-webpack-plugin": "^1.2.5",
"webpack": "^4.8.3",
"webpack-cli": "^2.1.4"
}
}
现在我怀疑这个问题与我的 tsconfig.json 文件中的行有关,但是如果我删除任何这些引用,我的项目将无法编译,因为显然我的一些类型是在这些库引用中定义的。"lib": [ "es5", "es2017", "dom"]
任何人都可以帮助指导我进入正确的方向,如何解决这个问题并在 TypeScript 中正确引用 DOM 和 React ASP.NET?
编辑: 我还尝试更改我的 tsconfig.json 文件以删除“lib”引用(假设我可以使用 Polyfill)到“lib”:[]。然而,问题仍然存在。
答:
由于上面的反馈,我相信我已经找到了解决问题的方法。
- 运行 'npm uninstall react -g' 从全局缓存中卸载 react 包。
在文件中创建对包的手动引用。
package.json
@types/react
“devDependencies”:{ “@types/反应”: “16.4.13” }
我最终package.json文件如下所示:
{
"name": "fungalai",
"version": "1.0.0",
"dependencies": {
"react": "16.4.2",
"react-dom": "16.4.2"
},
"devDependencies": {
"@types/flux": "3.1.7",
"@types/react": "16.4.13",
"axios": "^0.18.0",
"deep-diff": "^1.0.1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "^6.24.1",
"create-react-class": "^15.6.3",
"expose-loader": "^0.7.5",
"jszip": "^3.1.5",
"flux": "3.1.3",
"ts-loader": "^4.3.0",
"typescript": "3.0.1",
"uglifyjs-webpack-plugin": "^1.2.5",
"webpack": "^4.8.3",
"webpack-cli": "^2.1.4"
}
}
tsconfig.json像这样:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"jsx": "react",
"lib": [ "es6", "dom" ],
"removeComments": true,
"typeRoots": [
"/node_modules/@types",
"/Types/"
]
},
"compileOnSave": false,
"exclude": [
"/node_modules/",
"/bin",
"/obj"
]
}
这似乎已经解决了错误。
评论
@types/react
package.json
yarn why @types/react
我发现了以下 github 问题,我很确定它确定了您面临的问题的根本原因: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/25145
因此,您将在 16.3.9 版本中获得 @types/react,在 16.0.5 版本获得 @types/react-dom,因为您明确要求这些。但是,@types/react-dom 列出了对 @types/react 的依赖关系,如下所示:
"@types/react": "*"
[this] 解释为“最新版本的 @types/react”,因此它安装了该包的附加版本。
因此,您将同时使用类型包的版本 v16.3.9 和 v16.3.12,这会导致错误。
我知道有两种方法可以解决这个问题:
- 更新到package.json中最新版本的 @types/react 包
- 在package.json中添加一个 resolutions 部分,告诉 Yarn 以不同的方式解析依赖关系。
当您在不同版本中具有依赖项和具有相同依赖项的子模块时,会发生此错误。解决此问题的正确方法是在 .以下示例说明如何“解决”:
package.jsonpackage.json
{
"name": "project",
"version": "1.0.0",
"dependencies": {
"left-pad": "1.0.0",
"c": "file:../c-1",
"d2": "file:../d2-1"
},
"resolutions": {
"d2/left-pad": "1.1.1",
"c/**/left-pad": "^1.1.2"
}
}
文档参考:https://classic.yarnpkg.com/en/docs/selective-version-resolutions/
提供在我的情况下解决问题"skipLibCheck": true
tsconfig.json
compilerOptions
评论
.d.ts
在您的项目中,不同的包安装了不同版本的 .在 中,您可以为要解析到的这些类型指定特定版本,例如@types/react
package.json
// package.json
{
// ...
"dependencies": {
"react": "16.14.0",
"react-dom": "16.14.0",
// ...
},
"devDependencies": {
"@types/react": "^16.14.0",
"@types/react-dom": "^16.14.0",
// ...
},
"resolutions": {
"@types/react": "^16.14.0"
}
}
评论
global.d.ts