Alternate constructor syntax

Topics: Language Specification
Aug 7, 2013 at 1:19 PM
Edited Aug 7, 2013 at 4:35 PM
I'd like to propose a new syntax for constructors. Often I would like to have 'closure style' functions on the class (as opposed to prototype style) that close over the arguments of the constructor. Currently, the only way this is possible is to assign closure style functions in the constructor itself rather than as an initializer:
class MyController {
    static $inject = ["$scope"];
    constructor($scope: ng.IScope) {
        this.alertId = () => alert($scope.$id.toString());
    alertId: () => void;
This is cumbersome. Instead, I'd like to lift the constructor arguments to the class definition level, and then make them available to class initializers. The above angular controller would be reduced to the following:
class MyController($scope: ng.IScope) {
    static $inject = ["$scope"];
    alertId = () => alert($scope.$id.toString());
The constructor arguments are only available in initializers (the "closure style" functions) like the alert function above. They are not available within the body of a prototype function. A helpful error should be generated if you try.

Also, you would still be able to define a constructor, but you can only define constructor arguments in one place, either in the constructor function or lifted up to the class name. You would need to do this if you needed to call a base class constructor, for instance. If you lifted the constructor arguments into the class, the constructor function would need to have an empty argument list:
class BaseClass(_baseField: string) {
    baseField: string = _baseField;

class SubClass(_subField: string, _baseField: string) extends BaseClass {
    constructor() { super(_baseField); }
    subField: string = _subField;