简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM