How can moment.js be imported with typescript?

后端 未结 7 1545
-上瘾入骨i
-上瘾入骨i 2020-11-30 09:51

I\'m trying to learn Typescript. While I don\'t think it\'s relevant, I\'m using VSCode for this demo.

I have a package.json that has these pieces in it

相关标签:
7条回答
  • 2020-11-30 09:57

    Not sure when this changed, but with the latest version of typescript, you just need to use import moment from 'moment'; and everything else should work as normal.

    UPDATE:

    Looks like moment recent fixed their import. As of at least 2.24.0 you'll want to use import * as moment from 'moment';

    0 讨论(0)
  • 1. install moment

    npm install moment --save
    

    2. test this code in your typescript file

    import moment = require('moment');
    
    console.log(moment().format('LLLL'));
    
    0 讨论(0)
  • 2020-11-30 09:59

    You need to import moment() the function and Moment the class separately in TS.

    I found a note in the typescript docs here.

    /*~ Note that ES6 modules cannot directly export callable functions
    *~ This file should be imported using the CommonJS-style:
    *~ import x = require('someLibrary');

    So the code to import moment js into typescript actually looks like this:

    import { Moment } from 'moment'
    ....
    let moment = require('moment');
    ...
    interface SomeTime {
      aMoment: Moment,
    }
    ...
    fn() {
      ...
      someTime.aMoment = moment(...);
      ...
    }
    
    0 讨论(0)
  • 2020-11-30 10:00

    via typings

    Moment.js now supports TypeScript in v2.14.1.

    See: https://github.com/moment/moment/pull/3280


    Directly

    Might not be the best answer, but this is the brute force way, and it works for me.

    1. Just download the actual moment.js file and include it in your project.
    2. For example, my project looks like this:

    $ tree . ├── main.js ├── main.js.map ├── main.ts └── moment.js

    1. And here's a sample source code:

    ```

    import * as moment from 'moment';
    
    class HelloWorld {
        public hello(input:string):string {
            if (input === '') {
                return "Hello, World!";
            }
            else {
                return "Hello, " + input + "!";
            }
        }
    }
    
    let h = new HelloWorld();
    console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
    
    1. Just use node to run main.js.
    0 讨论(0)
  • 2020-11-30 10:14

    Still broken? Try uninstalling @types/moment.

    So, I removed @types/moment package from the package.json file and it worked using:

    import * as moment from 'moment'
    

    Newer versions of moment don't require the @types/moment package as types are already included.

    0 讨论(0)
  • 2020-11-30 10:17

    I've just noticed that the answer that I upvoted and commented on is ambiguous. So the following is exactly what worked for me. I'm currently on Moment 2.26.0 and TS 3.8.3:

    In code:

    import moment from 'moment';
    

    In TS config:

    {
      "compilerOptions": {
        "esModuleInterop": true,
        ...
      }
    }
    

    I am building for both CommonJS and EMS so this config is imported into other config files.

    The insight comes from this answer which relates to using Express. I figured it was worth adding here though, to help anyone who searches in relation to Moment.js, rather than something more general.

    0 讨论(0)
提交回复
热议问题