Y
Y
Yustas Alexu2015-05-27 20:55:29
Node.js
Yustas Alexu, 2015-05-27 20:55:29

How to get rid of namespace during module import?

There is a /lib/Response.ts module:

export class Response {
    /* bla-bla */
}

I call it like this in /app.ts:
import Response = require('./lib/Response');

var response = new Response.Response();

And I do not like this redundant Response.Response, how to make it so that you can simply:
import Response = require('./lib/Response');

var response = new Response();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Sivkov, 2015-06-08
@Yuxus

Why not import with the ES6 import mechanism? TypeScript supports it very well.

import {Response} from './lib/Response';
var response = new Response();

D
Dmitry Avilov, 2015-05-27
@TheCreator

I like this:
Module code

function newClass (settings) {
  return new Class(settings);
}
function Class(settings){
 this.settings = settings;
}
Class.prototype.someMethod = function () {
 var _this = this;
 console.log(_this.settings);
};
module.exports = newClass;

Main program code
var classInstance = require('./classFile.js')({'some':'settings'});
classInstance.someMethod();

But the options are endless, in fact, here are a couple of good examples discussing how to design modules:
bites.goodeggs.com/posts/export-this
darrenderidder.github.io/talks/ModulePatterns

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question