TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Importing momentjs using jspm

Open adrianhara opened this issue 11 years ago • 2 comments

I'm using TS 1.6.3 and JSPM. I jspm install-ed moment and then import it like so import * as moment from 'moment'

This compiles to

....
function(moment_1) {
    moment = moment_1;
}
....

Using Babel this compiles to

....
function(_moment) {
    moment = _moment['default'];
}
....

At runtime the moment_1 or _moment variable refers to an object which actually has a default property which is the MomentJS's factory. So, to me, it looks like Babel is correct.

As a result of the way the TS compiler compiles it, it's not possible, in TypeScript code, to simply do var x = moment() because at runtime that will fail as moment will be an object, not a function. You also can't do moment.default() because in the d.ts file there is no default function. At the moment I'm doing a hack (moment as any).default().

I would appreciate it if someone could shed some light on whether I'm doing something wrong or it's a bug.

Thanks!

adrianhara avatar Nov 16 '15 12:11 adrianhara

Ah, it looks like it's actually this: https://github.com/Microsoft/TypeScript/issues/5285

adrianhara avatar Nov 16 '15 12:11 adrianhara

Yes, this is a duplicate of #5285. A fix is underway in #5577.

ahejlsberg avatar Nov 16 '15 18:11 ahejlsberg