Reputation: 32808
I have this Javascript code running under node.js. I declare functions in more than one module and use an extend utility so the are available in others modules:
// File: util.js:
var Util = function () {
this.doTask1 = function(name) { return 9; };
}
module.exports = new Util();
// File: base.js:
var util = require('../../Utils/util.js');
var helpers = require('../../Utils/helpers.js');
var AdminBase = function () {
var self = this;
this.doTask2 = function(name) { return 99; };
}
module.exports = helpers.extend({}, util, new AdminBase());
// File: page.js:
var base = require('../Common/base.js');
var Page = function () {
base.doTask1()
base.doTask2()
// File: helpers.js
var extend = function (target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function (source) {
for (var prop in source) {
target[prop] = source[prop];
}
});
return target;
};
module.exports.extend = extend;
I now want to do the same using Typescript and I need some help. So far I understand that I need to code the importing of the modules like this:
// File: util.ts
module Util {
export function doTask1(name) { return 9; };
}
export = Util;
// File: base.ts
import util = require('../../Utils/util');
import helpers = require('../../Utils/helpers.js');
var AdminBase = function () {
var self = this;
this.doTask2 = function(name) { return 99; };
}
module.exports = helpers.extend({}, util, new AdminBase());
// File: page.js:
import base = require('../Common/base.js');
var Page = function () {
base.doTask1()
base.doTask2()
I am getting confused about how to code this so that I can access the doTask1() and doTask2() functions in the same way as I was doing for when I was using the first method.
Can someone give me advice and let me know how I can handle the exporting of functions so I can access these in the same way as I was doing before.
Note that what I don't want is to have to be coding:
util.doTask1()
base.doTask2()
as this starts to become confusing.
Upvotes: 0
Views: 431
Reputation: 275867
If you want to do
base.doTask1()
base.doTask2()
You need to export doTask1/doTask2 from base i.e.
// File: base.ts
import util = require('../../Utils/util');
import helpers = require('../../Utils/helpers.js');
export var doTask1 = util.doTask1;
export var doTask2 = function(name) { return 99; };
Upvotes: 2