Suppose there is a config.js file in which the constructor function is described:

function ConfigService () { this.greeting = function() {console.log('Hello World');} } 

What should a header file look like to import without errors?

  • What is a header file in terms of typescript? - Stepan Kasyanenko
  • Header or declarative typescript file - with the extension .d.ts, which, as I understand it, just serves to connect .js files. - Vladimir Mironov
  • Strictly speaking, .js files can be connected without .d.ts , only there will be no static typing. In your case, since this is a constructor function, you can describe a class. - Stepan Kasyanenko

2 answers 2

Understood. This method is suitable for those who use nodeJS. In the .js file, you must specify

 module.exports.ConfigService = ConfigService; 

In the .d.ts file, just export the function or class, if the constructor function is defined in the .js file:

 expot class ConfigService { smt: {}; } 

or

 export function ConfigService(): void; 

    In your case, since this is a constructor function, you can describe a class.

    Something like this:

     declare module 'my-module' { export class ConfigService { constructor(); greeting(): void; } } 

    See how and what you can create in the documentation .