This project is read-only.

Inheritance in TypeScript and the resulting code

Topics: General
Apr 4, 2014 at 10:23 AM
Hey everyone,

I really like the way you designed TypeScript. It addresses some features that are really missing from JavaScript, but still provide really useful features. What I really appreciate is the close mapping between the TypeScript code and the resulting JavaScript code which makes it attractive for framework authors as well, because even if the framework is written in TypeScript it's still absolutely usable with pure JavaScript if the user of the framework chooses to.

There's one area were this isn't true though in my opinion: Inheritance. If I write a framework and provide certain classes that the user has to inherit from to interact with the framework this is not really straightforward to do when the user is programming in JavaScript. The user would need to do the inheritance by herself, because the __extends function that is generated is in the scope of the generated scope – and even if the __extends function would be accessible from the user code, it would still be cumbersome to use.

Why did you choose to implement inheritance in that way and not provide an "extend method" as it is for example provided by Backbone.js? In that case the following code:
class Snake extends Animal {
would be translated into the following JS code:
var Snake = Animal.extend({
This is a) much easier to use for someone writing pure JavaScript and b) is much closer to the code written in TypeScript. Really interested in the design decision :)
Apr 4, 2014 at 4:50 PM
Interesting you brought up this point, as I have myself had to reference the "_extend" method in my framework in a way that end user's could use as part of the API. I did notice this issue exists:
...which suggests that even the global "_extends" routine needs some rethinking. Should it be left global and set on static types (sort of like "Type.extends = _extends"), or redefine the function for EVERY static type? (since there is no pre-existing static type that TypeScript can use for this purposes) Perhaps just define it on base types, and let the extend function copy the "extends" property forward to derived types. Just some things to think about. :)
Apr 4, 2014 at 5:42 PM
Edited Apr 4, 2014 at 5:43 PM
You can change the compiler to emit code in your style... Its not fun for all, but it can work. I changed compiler to emit code in ExtJS style:
Apr 4, 2014 at 8:10 PM
@jamesnw: Thanks for the pointer. It's good to know that they have it on the radar :)
@fabioparra: Excellent! I did not know that. Will look into it when I have the time!