Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple __extends output across various files #1350

Closed
whitneyit opened this issue Dec 3, 2014 · 3 comments
Closed

Multiple __extends output across various files #1350

whitneyit opened this issue Dec 3, 2014 · 3 comments

Comments

@whitneyit
Copy link
Contributor

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

@RyanCavanaugh
Copy link
Member

This has been a common request and we already had the notion of not emitting __extends when an implementation was in scope.

@whitneyit
Copy link
Contributor Author

Thanks for the feedback. Cheers

@whitneyit
Copy link
Contributor Author

This has been covered in #2901

@danquirk danquirk removed the Suggestion An idea for TypeScript label Jul 10, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants