0

I have some Javascript code that imports the messageformat module. This module can be used like this:

const MessageFormat = require('messageformat');
const mf = new MessageFormat("en-US");
const text = mf.compile(...);

This module exports a class, but it doesn't have a typings file. I created the following typings file:

declare module "messageformat" {
  export class MessageFormat {
    constructor(locale: string);
    public compile(messageString: string): string;
  }
}

In my Typescript code I now use it as:

import { MessageFormat } from "messageformat";
const mf = new MessageFormat("en-US");
const text = mf.compile(...);

Unfortunately, this doesn't generate new MessageFormat("en-US"), but it generates new messageformat_1.MessageFormat("en-US") which fails. I also tried the following approach:

declare module "messageformat" {
  export default class MessageFormat {
    constructor(locale: string);
    public compile(messageString: string): string;
  }
}

In my Typescript code I now use it as:

import MessageFormat from "messageformat";
const mf = new MessageFormat("en-US");
const text = mf.compile(...);

But this compiles to new messageformat_1.default('en-US') which is also not correct. How do I create the typings file (and how do I import the module) so the proper class is constructed?

Ramon de Klein
  • 5,172
  • 2
  • 41
  • 64

1 Answers1

0

This answer is based on this answer.

It looks that the following method works:

declare module "messageformat" {
  class MessageFormat {
    constructor(locale: string);
    public compile(messageString: string): string;
  }

  export = MessageFormat;
}

Unfortunately, I cannot use the import statement anymore, but the following code seems to work:

import MessageFormat = require("messageformat");
const mf = new MessageFormat("en-US");
const text = mf.compile(...);

Although I think the import MessageFormat = require("messageformat") looks a bit awkward, it does work and all typings can be used.

Ramon de Klein
  • 5,172
  • 2
  • 41
  • 64