我一直在尝试将 localForage 导入我使用 Aurelia-CLI 创建的 Aurelia TypeScript 项目中。根据有关添加引用的文档,我已经尝试以多种方式解决它。如果我只是在 aurelia.json 的依赖项中引用它,该库就会加载:
...
"localforage",
...
并将其导入:
import * as localForage from "localforage";
唯一的问题是我在编译时从 TypeScript 得到大量错误。诸如此类的用法:
localForage.getItem("user-settings").then((settings: UserSettings) => {
给我这样的错误:
src\repositories\settings-repository.ts(15,19): error TS2339: Property 'getItem' does not exist on type 'typeo
f "localforage"'.
[08:10:03] gulp-notify: [Error running Gulp] Error: src\repositories\settings-repository.ts(15,19): error TS23
39: Property 'getItem' does not exist on type 'typeof "localforage"'.
src\repositories\settings-repository.ts(19,23): error TS2339: Property 'setItem' does not exist on type 'typeo
f "localforage"'.
但随后继续在运行时工作。我的 typings.json 中确实定义了 localforage:
"localforage": "registry:dt/localforage#0.0.0+20160629074517"
TypeScript 似乎认为这实际上存在于 localForage.localforage 中,它在运行时会抛出未定义的异常,但不会出现编译时错误。我试过这样引用它:
import { localforage } from "localforage";
和(根据https://github.com/localForage/localForage上的文档)
const localForage:LocalForage = require("localforage");
两者都不再给出编译错误,但在运行时不起作用。