How can moment.js be imported with typescript?

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

提交回复
热议问题