【问题标题】:How do I access these functions from DefinitelyTyped in typescript?如何从打字稿中的 distinctlyTyped 访问这些功能?
【发布时间】:2020-10-03 15:29:49
【问题描述】:

我正在尝试创建一个适用于金额的 Angular2 应用程序。我正在尝试使用 dinero.js 来表示货币价值,但我很难使用 Typescript 中的某些功能。

按照说明,我使用此命令为dinero.js 安装了DefiniteTyped 映射

npm install @types/dinero.js --save

此库的类型映射声明为here

我像这样将库导入到我的代码中:

import * as Dinero from 'dinero.js';

这允许我使用工厂函数Dinero。问题是,该声明列出了一些我想使用但似乎无法使用的函数(即maximumminimum)。

如果我尝试Dinero.maximum(...),我会得到:

"export 'maximum' (imported as 'Dinero') was not found in 'dinero.js'

如果我尝试直接导入这些函数:

import {maximum, minimum} from 'dinero.js';

拨打maximum(...),我得到:

"export 'minimum' was not found in 'dinero.js'

我如何真正使用这些功能?

【问题讨论】:

  • 你必须像这样导入Dinero import Dinero from 'dinero.js'
  • 当我这样做时,我的 IDE 对我大喊This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag. 是启用这种不好的做法还是没问题?如果没问题,为什么默认禁用?
  • 不管怎样,我尝试启用该标志,现在我收到此错误:core.js:6260 ERROR Error: Uncaught (in promise): TypeError: dinero_js__WEBPACK_IMPORTED_MODULE_2__ is not a function

标签: angular typescript definitelytyped


【解决方案1】:

你需要先安装这些:

npm install dinero.js --save

npm install @types/dinero.js --save

然后像这样导入并使用它:

import  Dinero from "dinero.js";

const some_currency: Dinery.Currency = 'USD'
const some_val: Dinero.Dinero = Dinero({amount: 0, currency: some_currency})

这是正在运行的 stackblitz url: https://stackblitz.com/edit/angular-ivy-spxp5x?file=src%2Fapp%2Fapp.component.ts

【讨论】:

猜你喜欢
  • 2017-12-19
  • 2021-06-30
  • 2016-09-07
  • 2018-09-06
  • 2019-08-12
  • 2022-01-24
  • 2019-01-09
  • 2021-07-16
  • 1970-01-01
相关资源
最近更新 更多