【发布时间】:2019-02-06 01:05:21
【问题描述】:
我对使用 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"] 中的行有关,但是如果我删除任何这些引用,我的项目将无法编译,因为显然我的某些类型是在这些库引用中定义的.
谁能帮助我找到正确的方向来解决这个问题,并在 ASP.NET TypeScript 中正确引用 DOM 和 React?
编辑: 我还尝试更改我的 tsconfig.json 文件以删除“lib”引用(假设我可以使用 Polyfill)为“lib”:[]。然而问题仍然存在。
【问题讨论】:
-
您在“转到定义”中找到的
global.d.ts文件的完整路径是什么?假设这些来自 React 类型,看起来您可能安装了两个不兼容的 React 类型副本。 -
嗨,马特,感谢您的回复。完整路径为 C:\blah\blah\myproject\node_modules\@types\react 和 C:\Users\Max\AppData\Local\Microsoft\TypeScript\3.0\node_modules\@types\react 。现在我可以看到问题似乎是我的 node_modules 文件夹正在下载反应类型。如果我从 node_modules 中删除该文件夹并重新启动,它似乎确实可以解决,但问题是我没有看到我添加了反应类型参考的任何地方。
标签: asp.net asp.net-mvc typescript webpack definitelytyped