如何让 angular 编译器知道 npm/cordova 插件添加的类型定义?

How to let the angular compiler know about type definitions added by a npm/cordova plugin?

在我的 Ionic/Angular 项目中,我正在使用 SocialSharing-PhoneGap-Plugin. It has included type definitions,当我在 vscode 中使用它时,我看到了它们是如何被识别的。

我遇到的问题是 angular 编译器找不到它们。例如,使用:

window.plugins.socialsharing.shareViaEmail();

会抛出错误:

Property 'plugins' does not exist on type 'Window'.

如果我尝试将其映射为

const socialsharing: SocialSharing = (window as any).plugins.socialsharing;

我明白了

Cannot find name 'SocialSharing'.

如何让 Angular/Ionic 知道那些类型(生活在 node_modules/cordova-plugin-x-socialsharing/types/index.d.ts 中)?

解决方案是在我的 src 文件夹中添加新文件,其中包含对类型文件的三重斜杠引用

typings.d.ts

/// <reference path="../node_modules/cordova-plugin-x-socialsharing/types/index.d.ts" />

现在可以了!