user5047085
user5047085

Reputation:

Node.js exports syntax with TypeScript

Is there a way to accomplish this Node.js/CommonJS syntax with TypeScript?

const makeObservable = exports.makeObservable = function _makeObservable(fn: any, opts: any) {}

Basically as you can see, would like to declare a local variable on the same line as exporting that variable. Is it possible in strict TS?

Upvotes: 1

Views: 287

Answers (1)

stealththeninja
stealththeninja

Reputation: 3791

When exporting a named function, the compiled Javascript first defines the named function. You should be able to invoke the named function within the same file:

Example:

export function foo() {
    return 'bar';
}

const baz = foo();
console.log(baz);

compiles to CommonJS:

"use strict";
function foo() {
    return 'bar';
}
exports.foo = foo;
var baz = foo();
console.log(baz);  // bar

Upvotes: 1

Related Questions