admin管理员组

文章数量:1290927

I have Node-express code where modules are exported by using module.exports. For example, to export a function, it is written module.exports = functionName. Now the code will be converted to typescript. How to replace module.exports in typescript?

I have Node-express code where modules are exported by using module.exports. For example, to export a function, it is written module.exports = functionName. Now the code will be converted to typescript. How to replace module.exports in typescript?

Share Improve this question asked Jun 20, 2019 at 8:26 Sayan SahooSayan Sahoo 751 silver badge4 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

Just use export followed by the typical declaration, no matter whether it is a const, function, interface, enum, you name it.

export const myTestConst = () => console.log('hello world');

See https://www.typescriptlang/docs/handbook/modules.html

Adding up to duschsocke answer. You can also create a class with public methods and importing that class where you need those methods.

utils.ts

class Utils {

  public static publicFunction() {
    console.log('calling me');
  }
}

On other ts file:

import * as utils from 'utils';

// Call the function
utils.publicFunction(); // Prints 'calling me' on console.

In TypeScript, use export = functionName.

本文标签: nodejsConversion of moduleexports from javascript to typescriptStack Overflow