1
votes

I'm trying to create a Node.js module with a single namespace using TypeScript. I need the classes of the module to be in separate files, and have references to each other. I also need this module to utilize other node modules. But if I understand right, TypeScript only supports namespaces if a namespace is contained within a single file or the namespace does not use external modules.

Some people present the use of post-build tools to make the final module work, which is nice but doesn't address all the errors TypeScript throws when combining cross-file namespaces and imports during development.

Is it true that the closest solution is to create a module per file, and create a web of inter-imports?

1
I'm not following, as I've got something that sounds very similar working in a Windows App Store WinJs app. I didn't have any problems. - WiredPrairie

1 Answers

1
votes

I find that what works best for me for module dependencies is to always use source references, e.g.:

/// <reference path="other.ts" />

Since you can't generate single-file output from tsc if you use language-level imports/modules, I have resorted to using require as a function and so on.

This seems to be the same solution chosen by the TypeScript developers themselves (search for "require(" there). As suggested by silentorb's comment, you can declare the require function as some variation of the following (or use DefinitelyTyped):

declare function require(name: string): any;

Note that there's often a lot of discussion on the whole modularity topic in TS over on the TS forums (one recent example).