-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Class Expressions #497
Comments
Design Stress TestWe'll use this to explore some questions below function makeNode() {
var c = class Node {
self: Node;
constructor(n: string);
constructor(n: number);
constructor(private value: any);
}
return c;
}
function getNodeA() {
var n = makeNode();
return new n('hello');
}
function getNodeB() {
var n = makeNode();
return new n(42);
} Open questionsWhat is node.d.ts? How do private fields work? var x = getNodeA();
var y = getNodeB();
x = y; // OK? Our current rule for Fall-out from this would be that we need to emit the type of private fields, as well as allow annotating private fields in type literals. Do we start decomposing non-exported classes? module M {
var x = { n: 3 };
export var y = x;
} The logic is that because This same rule would now apply to classes, if class expressions and class declarations are actually interchangeable. The above comments about compatibility of private members become more important if this is the case, as you might expose a declared class's constructor function as if it were a class expression. |
This will be helpful for using TypeScript with AngularJS. Controllers are typically created as classes. Given the current lack of class expressions in TS this means a class declaration has to be separated from where it is passed to Angular. Not a massive hardship but it would be nice to go more idiomatic. 👍 |
Something that just came to mind: will constructors get contextually typed? |
Introduction
Currently, in TypeScript, we support only one of the two styles of creating classes in ES6 - that of class declarations. This proposal adds support for the other style, class expressions. This will fill our support for classes in ES6 and help bring us in alignment with both styles.
Class Expressions vs Class Declarations
Class expressions work a little differently than class declarations. Here's a list of the highlights:
Just as with class declarations, class expressions would create a constructor function that can be used to construct instances. Like class declarations, you may also declare property members and index members, as well as use the constructor parameters.
Examples
Emit
Class expressions, just like class declarations, create a constructor function. We would emit them similarly to how we do now, with the exception that they are not immediately bound to a variable. Instead, they create the function expression (IIFE) that builds the constructor function, and allows the developer to assign the result of this expression where appropriate.
Emit of .d.ts
The type emitted for class expressions is the object literal representation of the constructor functions, just as with other anonymous types.
Class expressions can also introduce recursion in the type literal. Anonymous types that have possibly recursive components (like the Node example above) will need special treatment, which should be part of a related spec (Edit: #517).
The text was updated successfully, but these errors were encountered: