Context: I am trying to create a definition file for a library that I am not the author. When consuming the definition file I created, TypeScript mention it cannot find the definition file.
I have tried several things, here are the last three:
First try (inspired by a similar library d.ts):
declare class MessageFormat {
constructor(message: string);
compile: (messageSource: string) => Msg;
}
export type Msg = (params: {}) => string;
export default MessageFormat;
Could not find a declaration file for module 'messageformat'. 'node_modules/messageformat/lib/messageformat.js' implicitly has an 'any' type.
Second try (from TypeScript's example to write d.ts)
declare class MessageFormat {
constructor(message: string);
compile: (messageSource: string) => MessageFormat.Msg;
}
declare namespace MessageFormat {
type Msg = (params: {}) => string;
}
export = MessageFormat;
Could not find a declaration file for module 'messageformat'. 'node_modules/messageformat/lib/messageformat.js' implicitly has an 'any' type.
Third try (from a GitHub question)
declare module "messageformat" {
export type Msg = (params: {}) => string;
export interface MessageFormat {
new(message: string): any;
compile: (messageSource: string) => Msg;
}
}
Cannot use 'new' with an expression whose type lacks a call or construct signature.
Code: The three tentatives are in this repo: https://github.com/MrDesjardins/importdefinitionfiles
Can someone give me a pointer about what am I doing wrong?
newwith it?npmand imported withimport MessageFormat from "messageformat";classwith aconstructoris correct. Also, looking at the source the constructor signature should be:constructor(locale: string|string[]|Object)and the Object should be replaced with a proper interface as well.exportkeywords in normal type definitions inside ambient module declarations. Everything is exported & declared by default.MessageFormatis the default export in this module isexport = MessageFormat;