Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript 1.5: ES6 Module default import of CommonJS 'export =' (.d.ts only issue?) [duplicate]

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?

like image 846
Matt Traynham Avatar asked Apr 12 '15 18:04

Matt Traynham


People also ask

Does TypeScript work with CommonJS?

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.

What is CommonJS vs ES6?

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.

What is default export in TypeScript?

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.


1 Answers

The syntax you are looking for

import * as moment from "moment";
like image 97
basarat Avatar answered Oct 11 '22 22:10

basarat