Search code examples
typescripttypescript1.8typescript-typings

How to create typings for ts


I have a library installed in node_modules/ and I want a fast hack so I can use it in typescript app. In folder typings/modules I created a folder with the name of the module and an index.d.ts file. Inside that file I have

declare module "lib-name" {
  export default class Logger {
    constructor(namespace: string)
  }
}

I'm able to import the module, but when I try to let l = new Lib('namespace'); I'm getting error cannot use 'with' an expression whose type lacks a call or construct signature


Solution

  • I don't think you should have class in your typings. It is an interface contract you should declare.

    Moreover the documentation says that the new expression needs a new method in the interface : https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html

    Try something like this maybe :

    declare module "lib-name" {
      interface Logger {
        new (namespace: string): Logger
      }
    
      export var Logger: Logger;
    }