Cleaner way to get moment working with Typescript and Aurelia?

I’m using Aurelia with TypeScript and trying to import momentjs. I had this working in older (non-TypeScript) projects with the line import moment from 'moment'. However, this doesn’t seem to work with TypeScript.

I then found this link and one of the examples showed the following:

import * as moment from 'moment';

export class DateFormatValueConverter {
    toView(value){
        return (moment as any).default(value).format("MM DD, YYYY");
    }
} 

That works – but wow is it ugly. Is there a cleaner way to work with momentjs and TypeScript within Aurelia?

1 Like

I usually do barrel export for difficult vendor libs with difficult typings:

// vendor.moment.ts
import * as moment from 'moment';

export { moment }
1 Like

TS has a compiler option esModuleInterop to simplify import since v2.7.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html

Turn it on in tsconfig.json like this:

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}
1 Like

Yes, for node app i would use this because its full of this kind in node development. Though i think its not the best way to do it as it hides whats really being exported from the target module