Skip to content

Suggestion: support ClassExpression (as per ES6) #286

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

Closed
fsoikin opened this issue Jul 28, 2014 · 2 comments
Closed

Suggestion: support ClassExpression (as per ES6) #286

fsoikin opened this issue Jul 28, 2014 · 2 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@fsoikin
Copy link

fsoikin commented Jul 28, 2014

The ES6 definition of the "class" construct has the concept of "class expression" - i.e. class definitions may be used as values, such as:

var obj = new (class { x = 5; });
var cls = class { x = 5; };
var cls2 = class extends cls { y = 6; };
var obj2 = new cls2();
// and so on

Sadly, this support is absent in the current TypeScript compiler (I'm using the 9.2 branch) - that is, the above examples would not compile.

Aside from TS team's own argument of "closely aligning with the ES6 specification", this support would enable many useful scenarios for which I currently have to fall back to "any-typed" plain JavaScript.

Here are a few examples of such scenarios which I actually use in my daily work:

  1. Returning a class (not object) from a function. With TypeScript, it is currently possible to describe the return type of such function, but not to actually write its body using the "class" construct.
  2. "Bind" a class - similar to "binding" a function, i.e. produce another class with certain constructor parameters bound to specific values.
  3. Declare a "private" class within the body of a function or within another class.
@danquirk
Copy link
Member

This is definitely on the docket. We've talked about it in some design meetings already but do not yet have a full proposal.

@RyanCavanaugh
Copy link
Member

Duplicate #239

@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
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants