如何使用 chrome-app.d.ts 输入 angular-cli 1.0.1 应用程序?

How to use chrome-app.d.ts type in angular-cli 1.0.1 app?

我已经安装了@types/chrome并添加到tsconfig.app.json

"types": [
  "chrome"
]

但是当我在组件中使用chrome.app时,它显示错误:

Property 'app' does not exist on type 'typeof chrome'

@types/chrome 文件夹包含 chrome-app.d.ts 是我需要的文件, 我怎么参考这个

来源https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/chrome

由于此类型定义模块的创建者选择不在 @types/chrome 模块的 index.d.ts 文件中包含 chrome.app.* 定义,而是将它们分开到 [=13] =],您需要在使用这些 API 的文件中明确引用它:

/// <reference types="chrome/chrome-app"/>

请注意,根据 TypeScript 中三斜杠指令的文档,这必须位于文件顶部:

https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

另请注意,不需要在您的tsconfig.json文件中添加"types": ["chrome"]引用,因为最新版本的TypeScript编译器应该自动 导入在node_modules/@types 中找到的所有类型定义模块。通过将此添加到您的配置中,您告诉它 导入该模块。有关更多信息,您可以参考配置文档:

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

现在(2018 年 6 月)Angular CLI 的更新(Angular 6)你可以简单地做:
npm i @types/chrome
然后错误就会消失。