Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume the Moment.js TypeScript definition file if my site is already using moment.min.js?

I'm in the process of converting a website to use TypeScript and I'm converting just one of many JavaScript files to TypeScript. All pages of my site already reference moment.js, such as:

<script src="/scripts/moment.min.js"></script>

I've added other TypeScript definition files using:

npm install --save-dev @types/jquery

... But, the above seems like the wrong choice for moment. When I use the above command (but substitute 'moment' for 'jquery'), a readme file is downloaded that says:

This is a stub types definition for Moment (https://github.com/moment/moment). Moment provides its own type definitions, so you don't need @types/moment installed!

As a solution, I tried saving the moment.d.ts file from its GitHub repo and referencing it in the TypeScript file as so:

///<reference path="../../Scripts/typeDeclarations/moment.d.ts"/>
var now:any = moment.utc();

But, TypeScript gives me the warning:

Cannot find the name 'moment'

like image 844
edt Avatar asked Feb 14 '17 18:02

edt


People also ask

How to import moment JS with typescript?

To import moment.js with TypeScript, we can import the whole package with import. to import the 'moment' module as moment. To import moment.js with TypeScript, we can import the whole package with import.

How to use moment JS in a project?

Just download the actual moment.js file and include it in your project. $ tree . ├── main.js ├── main.js.map ├── main.ts └── moment.js Just use node to run main.js. Show activity on this post. I've just noticed that the answer that I upvoted and commented on is ambiguous. So the following is exactly what worked for me.

Do I need @types in moment?

Show activity on this post. Apparently, moment now provides its own type definitions (according to sivabudh at least from 2.14.1 upwards), thus you do not need typings or @types at all. import * as moment from 'moment' should load the type definitions provided with the npm package.


1 Answers

TL;DR;

Add the moment.d.ts (link) in your project from the moment github site and comment out the last line

 //export = moment;

I have created a mini test project to prove this and this works for me.

Project structure

--
  |
  -typings/
     -moment/
       -moment.d.ts   <-- //export = moment; commented out
  -typescript/
     -main.ts
  -tsconfig.json

Contents of both typings and typescript are part of the compilation target, i.e. not excluded in tsconfig.json which looks like this

{
    "compilerOptions": {
        "target": "es5",
        "declaration": false,
        "noImplicitAny": true,
        "removeComments": true,
        "outDir": "dist",
        "jsx": "react",
        "sourceMap": true,
        "experimentalDecorators": true
    },
    "compileOnSave": true,
    "exclude": [
        "node_modules",
        "dist"
    ]
}

The main.ts file contains

const now: moment.Moment = moment.utc();

and compiles... As expected, my IDE gives me auto-completion from the definition file. (no need for ///<reference tags - you should avoid them anyway)

Removing the export statement at the end of the definition file, "transforms" it into an internal - but global - module declaration file.

like image 64
Bruno Grieder Avatar answered Oct 16 '22 14:10

Bruno Grieder