【问题标题】:How to use moment-duration-format in TypeScript?如何在 TypeScript 中使用 moment-duration-format?
【发布时间】:2017-08-20 21:25:24
【问题描述】:

我在我的 TypeScript (Ionic2/Angular2) 项目中使用 moment.js。继this post之后,我现在想尝试一个插件moment-duration-format

我有 npm 包和类型定义,可以通过 import as.. 使用。

import moment from 'moment';
...
let duration = moment.duration(decimalHours, 'hours');

我现在想使用moment-duration-format

我通过npm install moment-duration-format --save 安装,然后通过npm i @types/moment-duration-format --save 定义类型。

我可以看到两个 npm 模块。

与往常一样,如何使用此类类型定义总是有一些谜团(包括导入在内的用法似乎从未出现在任何文档中)。

我已尝试添加import 'moment-duration-format';import duration from 'moment-duration-format';(moment-duration-format/index.d.ts' 不是模块。)

尝试如下使用时出现错误..

let dd = moment.duration.format(400.99, 'hours').format('D:HH:mm');

// (TS error [ts] Property 'format' does not exist on type '(inp?: DurationInputArg1, unit?: DurationConstructor) => Duration'.

有没有人知道如何在 TypeScript 中使用它。

提前致谢

【问题讨论】:

    标签: javascript angular typescript momentjs


    【解决方案1】:

    我使用了解决方法here,但现在看起来 ype def 已修复。获取 def 类型的更新所以我可以执行以下操作...

    import * as moment from 'moment';
    import 'moment-duration-format';
    
    let duration = moment.duration(decimalHours, 'hours') ;   
    let options : moment.DurationFormatSettings   = {
      forceLength : false,
      precision : 0,
      template : formatString,
      trim : false
    };
    let result  = duration.format(formatString, 0, options);
    

    【讨论】:

    • 你是如何更新类型定义的?哪个版本?我使用的是最新版本(moment 2.29.1 和 moment-duration-format 2.3.2),但仍然遇到类型问题。
    【解决方案2】:

    您使用duration 作为属性而不是调用它。试试:

    let dd = moment.duration(400.99, 'hours').format('D:HH:mm');
    

    这实际上不是 TypeScript 问题。它也不适用于 JavaScript。在 JavaSctript 中,您会遇到运行时错误,而 TypeScript 不允许您在编译时执行此操作(证明其价值)。

    【讨论】:

    • 谢谢你,但仍然无法让它工作,因为不知道如何导入。我有import moment from 'moment'; import 'moment-duration-format';,然后尝试上述方法,但我得到了 TS 错误[ts] Property 'format' does not exist on type 'Duration'。我只是无法让它“看到”moment-duration-format 插件。也试过import duration from 'moment-duration-format';,但这也不起作用。
    • 你需要安装打字定义“@types/moment-duration-format”
    • 和 --save-dev ;)
    【解决方案3】:

    我也遇到了同样的问题...

    似乎类型定义中有错误。其他人也有同样的问题,你可以在这里看到:https://github.com/souldreamer/noti-cli/blob/ef104c22792e0dfeb67d3372b04e231d45ffaa55/src/shared/pipes.ts#L38

    作为 github 上的拉取请求的一部分,该问题已得到修复,但不幸的是,该问题已在未合并的情况下关闭: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/14327

    作为临时解决方法,您可以使用第一个链接的代码行。作为第二步,我们应该尝试在 github 上创建一个新的拉取请求,以便在以后的版本中解决问题。

    【讨论】:

      【解决方案4】:

      问题在于 R_B_ 正确地在类型模块中为 moment-duration-format 制定了错误的类型定义。

      我将为此创建一个新的拉取请求,因为我的最后一个请求因 lint 错误而关闭。

      只要你可以在你的项目中使用自定义类型定义,就像这里所做的那样 https://github.com/TwoStone/leaderboard/blob/develop/subprojects/webapp/src/custom-typings.d.ts 只需在您的项目中创建文件,它应该可以暂时使用。

      【讨论】:

        猜你喜欢
        • 2015-08-01
        • 1970-01-01
        • 2017-05-13
        • 2020-11-16
        • 2016-07-11
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        • 2018-01-14
        相关资源
        最近更新 更多