在 Angular 库中使用带有 Ng-Packagr 的全局

Using a global with Ng-Packagr in an Angular library

鉴于我们正在尝试在使用 ng-packagr 构建的 Angular 项目中引用 Lodash,我们收到以下错误:

error TS2686: '_' refers to a UMD global, but the current file is a module. Consider adding an import instead.

这表明它没有正确获取我们设置的打字稿定义文件。

该错误仅在使用ng-packagr 构建时出现。使用 ng-serve 测试演示应用程序时,它工作正常。

typings.d.ts

/* SystemJS module definition */
declare var module: NodeModule;
interface NodeModule {
  id: string;
}

// lodash global typing - begin
declare namespace _ {
}
// lodash global typing - end

我在 ng-packagr 的 github 页面上看到了各种关于使用外部或嵌入式的问题,但文档非常稀少,而且在这方面的实验还没有为我提供任何线索供我遵循。

我看到这个片段弹出了几次,它似乎准确地引用了正确的路径。

ng-package.json

...
    "externals": {
      "lodash": "./node_modules/lodash/index.js"
    },
...

有人知道我可以如何调试和调查这个问题吗?

我找到了解决此问题的方法,但我对解决方案不满意。

在下面的link中,ng-packagr 的作者建议使用三重斜杠引用来引用.d.ts。

https://github.com/dherges/ng-packagr/issues/908