I'm encountering an issue with:
import moment from 'moment';
moment
itself is a function that is a default CommonJS export, as coded here https://github.com/borisyankov/DefinitelyTyped/blob/master/moment/moment.d.ts:
interface MomentStatic {
(): Moment;
(date: number): Moment;
...
}
declare var moment: moment.MomentStatic;
declare module 'moment' {
export = moment;
}
The following do not seem to work:
import * from 'moment';
// error TS1005: 'as' expected.
// error TS1005: 'from' expected.
import moment from 'moment';
// error TS1192: External module ''moment'' has no default export.
import {default as moment} from 'moment';
// error TS2305: Module ''moment'' has no exported member 'default'.
The require syntax still works... but I'm trying to avoid that.
import moment = require('moment');
Thoughts?
Yes, you can use it in a same manner that you would use it in Javascript. Typescript is superset of Javascript, all things possible in Javascript are also possible in Typescript.
ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.
export = and import = require()Default exports are meant to act as a replacement for this behavior; however, the two are incompatible. TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module.
The syntax you are looking for
import * as moment from "moment";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With