Lev Kuznetsov Lev Kuznetsov - 2 months ago 12
TypeScript Question

Is there a way to check if the current export is imported in typescript?

In commonjs (node) I can do this:

module.exports = {...}
if (module.parent) {...}


Is there something similar in typescript? I'm writing a web app and it would allow me to not bind a port during tests.

Answer Source

TypeScript is not responsible for providing such functionality. It is entirely the responsibility of your module loader (e.g. Node's built-in module loading code, SystemJS, RequireJS) or bundler (e.g. Webpack, Browserify) to provide such functionality. If you compile your TypeScript code and run it in Node, then yes, module.parent will be there though it is not declared in the TypeScript declarations by default. If you install the NPM package @types/node, you'll get a declaration for it though. (npm install @types/node) Or you can declare it yourself if you prefer.