Reputation: 75
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
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
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