【问题标题】:How to properly import localForage in Aurelia-Cli typescript project如何在 Aurelia-Cli typescript 项目中正确导入 localForage
【发布时间】:2016-08-09 15:16:38
【问题描述】:

我一直在尝试将 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");

两者都不再给出编译错误,但在运行时不起作用。

【问题讨论】:

  • 嘿,你解决了吗?

标签: typescript localforage aurelia


【解决方案1】:

使用泛型

您第一次拍摄时大部分时间都在那里,但查看here 的打字,您可以看到正确的语法是getItem<T>。所以试试这个:

localForage.getItem<UserSettings>("user-settings")

【讨论】:

  • 第一个,import * as localForage from "localforage";实际上确实在代码中工作。问题是 TypeScript 似乎希望它为 "localforage.localforage.getItem("xxx")..." 但它实际上只是 "localforage.getItem("xxx")..." 所以第一个工作至使用 localforage 可以,但它在编译过程中对我大喊大叫。
  • 是的,它没有用。 Typescript 认为应该是 localForage.localforage.getItem("user-settings")
  • 看起来打字不好,在localforage github上开一张票。
猜你喜欢
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
相关资源
最近更新 更多