Description
Given the following files
// animal.ts
class Animal {}
export = Animal;
// mammal.ts
import Animal = require('./animal');
class Mammal extends Animal {}
export = Mammal;
// human.ts
import Mammal = require('./mammal');
class Human extends Mammal {}
export = Human;
And the following command, either --module
flag will work for this example
$ tsc --module amd human.ts
The following files are output
define(["require", "exports"], function (require, exports) {
var Animal = (function () {
function Animal() {
}
return Animal;
})();
return Animal;
});
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
define(["require", "exports", './animal'], function (require, exports, Animal) {
var Mammal = (function (_super) {
__extends(Mammal, _super);
function Mammal() {
_super.apply(this, arguments);
}
return Mammal;
})(Animal);
return Mammal;
});
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
define(["require", "exports", './mammal'], function (require, exports, Mammal) {
var Human = (function (_super) {
__extends(Human, _super);
function Human() {
_super.apply(this, arguments);
}
return Human;
})(Mammal);
return Human;
});
As you can see, a lot of repetition of the __extends
functions starts to appear when working with a larger codebase.
My question is this, would the TypeScript team be open to a flag that can be passed to tsc
that will generate something lke the following
define(["require", "exports", "__extends", './mammal'], function (require, exports, __extends, Mammal) {
var Human = (function (_super) {
__extends(Human, _super);
function Human() {
_super.apply(this, arguments);
}
return Human;
})(Mammal);
return Human;
});
This will be an option that will need to be bought into by the user executing the tsc
command. This is not a suggestion to refactor the existing --module
flag, maybe --customExports
.
The onus would then be on the develop to wire up an __extends
module themselves. A module that could be reworked to suit there needs should they see fit. See #1193. Some boilerplate could be generated and printed to stdout
similar to the way that behat squawks about missing test snippets.
// You can implement the __extends function with the following snippet.
define('__extends', function () {
return function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
});
The advantage of this solution is the obvious reduction in repetitive code, as well allowing customisation of the __extends
function, and helping teams hit that elusive 100% coverage.
Some references
https://typescript.codeplex.com/workitem/2002
http://stackoverflow.com/questions/22155106