How can moment.js be imported with typescript?

后端 未结 7 1548
-上瘾入骨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 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.

提交回复
热议问题