Sayan Sahoo
Sayan Sahoo

Reputation: 75

Conversion of module.exports from javascript to 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?

Upvotes: 2

Views: 5457

Answers (3)

Patrick Roberts
Patrick Roberts

Reputation: 51756

In TypeScript, use export = functionName.

Upvotes: 0

goediaz
goediaz

Reputation: 652

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.

Upvotes: 4

wfreude
wfreude

Reputation: 508

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.org/docs/handbook/modules.html

Upvotes: 6

Related Questions