"error TS2694: Namespace 'angular' has no exported member 'xxx'" 升级后@types/angular

"error TS2694: Namespace 'angular' has no exported member 'xxx'" after upgrading @types/angular

将angular和@types/angular版本升级到版本1后。6.x我有很多TS2694错误:

error TS2694: Namespace 'angular' has no exported member 'material'
error TS2694: Namespace 'angular' has no exported member 'ui'
error TS2694: Namespace 'angular' has no exported member 'translate'

这在更改版本号之前工作正常。

什么可能导致问题?

问题与我 node_modules 中的几个 angular 类型定义有关。 Angular 类型在 node_modules/@types/angularnode_modules/@types/ng-file-upload/node_modules/@types/angular 中定义。

这是因为 yarn 用不同的版本解析 angular。我在 yarn.lock 文件中有两个不同分辨率的 angular 条目:

"@types/angular@*":
  version "1.6.7"
  resolved "https://registry.yarnpkg.com/@types/angular/-/angular-1.6.7.tgz#8935a2b4a796fe7ca4f59f533f467804722fb0c4"
  dependencies:
    "@types/jquery" "*"

"@types/angular@1.6.x":
  version "1.6.32"
  resolved "https://registry.yarnpkg.com/@types/angular/-/angular-1.6.32.tgz#fc791aad038227d9413eb5e552993e1076f8a509"

"@types/ng-file-upload@^11.1.31":
  version "11.1.34"
  resolved "https://registry.yarnpkg.com/@types/ng-file-upload/-/ng-file-upload-11.1.34.tgz#670fd0515c8e08668b27b7bbe30356b3b8011780"
  dependencies:
    "@types/angular" "*"

删除 yarn.lock 并重新运行 yarn install 解决了问题,但在我的案例中它修改了太多其他依赖项。

使用 yarn install --flat 可能会解决问题,但我不想更改所有依赖项的解析方式。

所以我通过手动将 yarn.lock 文件更改为

解决了这个问题
"@types/angular@*", "@types/angular@1.6.x":
  version "1.6.32"
  resolved "https://registry.yarnpkg.com/@types/angular/-/angular-1.6.32.tgz#fc791aad038227d9413eb5e552993e1076f8a509"

"@types/ng-file-upload@^11.1.31":
  version "11.1.34"
  resolved "https://registry.yarnpkg.com/@types/ng-file-upload/-/ng-file-upload-11.1.34.tgz#670fd0515c8e08668b27b7bbe30356b3b8011780"
  dependencies:
    "@types/angular" "*"

像上面接受的答案那样手动编辑文件给我带来了一些问题。

运行 "yarn upgrade" 或更改某些包会恢复手动编辑的更改,我的应用程序将再次损坏。

为了解决这个问题,我向 package.json 添加了一个解决方案,解决了解决不同版本的问题。

"resolutions": {
  "**/@types/angular": "1.6.20"
 },

我不得不卸载所有节点模块并重新安装它们, 卸载类型 rm -r node_modules clean-node-modules 安装只需输入 npm install