0

The simple question here is how do I use namespaced typescript classes as external modules for use in node. The answer to that seems to be

It can't be done, don't try it, don't use namespaces

Well I am in the position that we have a large codebase that is all written in namespaces and we are now trying to use this code in node.js as well.

I am trying to figure out how to use our existing code.

I have got as far as compiling my namespaced code into a commonjs nodelibs folder that produces one js file and one d.ts for each ts file.

I have to add the following to each of my ts files that I want to be available to node:

namespace Organisation.Project.Entities{
    export class SomeThing{

    }
}

var module: {copy module definition from node.d.ts}

if(module)
{
    module.exports = Organisation.Project.Entities;
}

I can use this code in my node application:

var entities = require("../nodelibs/entities/entities");

var myThing = new entities.SomeClass();

despite there being both a entities.js and entities.d.ts at ../nodelibs/entities/entities there is no type info or compile time checking that SomeClass is a thing.

I can manually import the references file and manually type the instance of the class but in my case SomeClass is a class with a lot of statics on it so I can't fingure out how to type it properly...

var entities = require("../nodelibs/entities/entities");

var classRef = entities.SomeClass;

var myVar = classRef.staticProperty;

In this example I can't type classRef as SomeClass as it isn't an instance of SomeClass, it's the class constructor. How do I type classRef so that I can get access to the static properties on the class Some Class?

Thanks

3
  • Maybe with import entities instead of var entities? Commented Aug 31, 2016 at 15:15
  • Import gives various errors along the lines of cannot find module or entities.js is not a valid module... Commented Aug 31, 2016 at 15:17
  • In the imported file entities.ts, you should use the standard static ES6 syntax: export namespace Organisation.Project.Entities { /* ... */ }. After that, the compiler will be able to see it as a valid module. Commented Aug 31, 2016 at 15:20

1 Answer 1

1

Write the code with ES6 modules

In the module, use the static ES6 module syntax:

// File entities.ts
export namespace Organisation.Project.Entities {
    export class SomeClass {
        public static abc = 'abc';
    }
}

Then, import your namespace:

// File main.ts
import {Organisation} from './entities';
type classRef = Organisation.Project.Entities.SomeClass;
var myVar = classRef.abc;

Compile for Node.js

Say the compiler to generate modules in the CommonJS format:

// File tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs"
    },
    "exclude": [
        "node_modules"
    ]
}

Compile for the browser

In a browser, you can use a bundler or a loader. There is some configuration to do.

Bundlers:

Loaders:

Sign up to request clarification or add additional context in comments.

2 Comments

So close! I thought that was going to fix it but if I add export to the namespace then I can't compile without modules for use in the browser (with outFile specified). This is how we currently compile our codebase, all into one file. Thanks
@Roaders : You can use a bundler (Webpack) or a loader (SystemJS) in order to work with a browser. I'll edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.