Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can moment.js be imported with typescript?

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:

{   "devDependencies": {     "gulp": "^3.9.1",     "jspm": "^0.16.33",     "typescript": "^1.8.10"   },   "jspm": {     "moment": "npm:moment@^2.12.0"   } } 

Then I have a Typescript class main.js like this:

import moment from 'moment'; export class Main { } 

My gulpfile.js looks like this:

var gulp = require('gulp'); var typescript = require('gulp-tsb'); var compilerOptions = {                         "rootDir": "src/",                         "sourceMap": true,                         "target": "es5",                         "module": "amd",                         "declaration": false,                         "noImplicitAny": false,                         "noResolve": true,                         "removeComments": true,                         "noLib": false,                         "emitDecoratorMetadata": true,                         "experimentalDecorators": true                       }; var typescriptCompiler = typescript.create(compilerOptions); gulp.task('build', function() {   return gulp.src('/src')     .pipe(typescriptCompiler())     .pipe(gulp.dest('/dest')); }); 

When I run gulp build, I get the message: "../main.ts(1,25): Cannot file module 'moment'."

If I use import moment = require('moment'); then jspm will work and bring in the module when I run the application, but I'm still receiving the build error. I also tried:

npm install typings -g typings install moment --ambient --save 

Instead of making the problem better though, it got worse. Now I get the above error on build as well as the following: "../typings/browser/ambient/moment/index.d.ts(9,21): Cannot find namespace 'moment'."

If I go to the file provided by typings and add at the bottom of the file:

declare module "moment" { export = moment; } 

I can get the second error to go away, but I still need the require statement to get moment to work in my main.ts file and am still getting the first build error.

Do I need to create my own .d.ts file for moment or is there just some setup piece I'm missing?

like image 473
peinearydevelopment Avatar asked Apr 15 '16 13:04

peinearydevelopment


People also ask

How do you define a moment in TypeScript?

To use Moment. js with TypeScript, we can use the types provided by moment . import * as moment from "moment"; Then in the getDate method, we set the return type of it to moment.

Can I use MomentJS in angular?

The moment object lives on window in the browser. Therefor it is not correct to import it in your angular2 application. Instead include the <script> tag in your html that will load the moment. js file.

How do I use moment timezone in TypeScript?

Use Typescript @types packages and import it via import * as moment from 'moment-timezone'; You can use all moment methods and member vars as moment-timezone exports them. Show activity on this post. Show activity on this post.

What can I use instead of moment in JavaScript?

There are several libraries out there that can potentially replace Moment in your app. The creators of Moment recommend looking into Luxon, Day. js, date-fns, js-Joda, or even replacing Moment with native JS.


1 Answers

Update

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.

That said however, as said in moment/pull/3319#issuecomment-263752265 the moment team seems to have some issues in maintaining those definitions (they are still searching someone who maintains them).


You need to install moment typings without the --ambient flag.

Then include it using import * as moment from 'moment'

like image 109
Aides Avatar answered Oct 27 '22 17:10

Aides