Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap many internal modules for exporting in typescript

I am looking at using typescript within node, and am currently used to using typescript via the ///<reference.../> syntax purely using internal modules. However with larger projects this can get unwieldy as you can have modules referencing other modules which all have interlinking references.

So for this node project I was thinking about trying to group all logical components as internal modules/classes much like before, so they will all internally reference each other, but expose them via one external module which would expose the underlying classes etc.

This way the syntax would be very similar of nodes existing requiring mechanisms, like:

import database = require("my-external-db-module.ts");
var connection = new database.Connection(someUrl);

rather than

///<reference path="my-internal-db-modules.ts" />
var connection = new Database.Connection(someUrl);

And I imagine the syntax would be something like:

///<reference path="all-my-internal-module-files-etc.ts" />
///<reference path="..." />
export module SomeExposingModule
{
   // Not quite sure what to put in here to expose the internal modules
}

So are there any sort of best practices around this sort of thing or any others who have done something similar, or does everyone just stick to using internal modules for complex stuff?

like image 233
Grofit Avatar asked Jul 18 '13 09:07

Grofit


People also ask

How do I export multiple functions from TypeScript?

Use named exports to export multiple functions in TypeScript, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

Can you have more than one module exports?

Description. Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

How do I import all modules into a directory in TypeScript?

To import all modules from a directory in TypeScript, we can create a module that reexports all modules that were imported. export { default as A } from "./a"; export { default as B } from "./b"; to import the default exports from modules a and b .

What are internal modules in TypeScript?

Internal Module Internal modules were in the earlier version of Typescript. It was used for logical grouping of the classes, interfaces, functions, variables into a single unit and can be exported in another module. The modules are named as a namespace in the latest version of TypeScript.


1 Answers

I am not sure if this is bad practice or not but here is how I solved my problem.

First a quick summary of the problem again:

I have multiple files all logically grouped under a namespace, for example Framework then all files under there will be Framework.*, such as Framework.Database or Framework.UnitOfWork. Then these are all compiled via tsc --out framework.js ... so I get all of this outputted into a framework.js file.

Now the above sounds fine, however it does not allow you to export modules when using --out because it spans multiple files, so for node to work I need to export the modules somehow, so I basically appended an extra typescript file which manually does this for me in the compilation:

// exporter.ts
module.exports = Framework;

So providing this is the last file added to the tsc compilation you will end up with something like:

// Framework.js
var Framework;
(function (Framework) {
    // lots of good stuff
})(Framework || (Framework = {}));
module.exports = Framework;

So this will export the internal modules fine and the export declaration will be included now because of the exporter.ts file which is now included.

So I am not sure if this is bad practice but this allows me to have the best of both worlds, a re-usable module which is laid out with namespaces and is spread across a sensible file structure, and a compiled single module which can be included either by references or by nodejs require.

So the usage would look like:

var Framework = require("./framework");
var database = new Framework.Database.DbConnection();
like image 99
Grofit Avatar answered Oct 01 '22 02:10

Grofit