angular 2 个 websql 类型

angular 2 websql typings

我正在使用 angular 4. 我需要一个数据库并想使用 websql,但我找不到导入 websql 类型的方法。

我添加了@types/websql。在我的 IDE 中,当我这样做时没有编译错误:

const db: Database = window.openDatabase('foo', '1.0', 'foo', 2 * 1024 * 1024);

但是 ng serve 给我:

Property 'openDatabase' does not exist on type 'Window'

我没有任何特定于 @types/websql 的导入。因为它不是模块,所以我不知道如何导入它。

有人知道我如何导入这个吗?

好的,我找到了解决方案。

ng-cli 生成 tsconfig.app.json,属性 types 设置为 []。 如果我正确理解 tsc 文档,它会阻止编译器使用 typeRoots 属性.

通过简单地删除这个 属性,我的代码编译。

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

all visible “@types” packages are included in your compilation ...

If typeRoots is specified, only packages under typeRoots will be included.

If types is specified, only packages under types will be included.

如果我把类型放在 ts.config.app 这样的配置中:

"types": [
  "websql"
] 

它将只包含 node_modules/@types/websql,忽略其他可能已安装的。