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

Extending class 'extends' to work with expressions #239

Closed
RyanCavanaugh opened this issue Jul 24, 2014 · 2 comments
Closed

Extending class 'extends' to work with expressions #239

RyanCavanaugh opened this issue Jul 24, 2014 · 2 comments
Labels
Duplicate An existing issue was already created Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

Example

class B extends class C { 
    constructor() {
        this.y = 4;
    }
} {
    constructor() {
        this.x = 3;
    }
}

Codegen

Should be able to unpack the class expressions and codegen as we do now.  We can pass the resulting expression as the parameter to the codegen’d constructor function as we do already.

Typecheck

The type of the expression follows the same rules we have for class declarations now. The expressions create constructor functions as before.

In the extends position, e.g. "class A extends B", the extends clause must be an expression, or a Generic NamedTypeReference. If it is an expression, the extends clause uses the type of the expression. This type must have a constructor signature. 

This would allow it to be compatible with the current system and be more flexible with arbitrary expressions, as we would treat class name TypeReferences as constructor functions we will get the type of.

Questions

 If C is visible outside of the scope, then we need to lift out and make visible.  Else, we can just translate the expression and put it into the call to creating B’s constructor function

Traceur says it’s not in scope outside of B’s expression.  Looks like we may be able to translate the expressions in-place as a call to the constructor function
 

If we base the typecheck on constructor signature, what about overloaded constructor signatures?

We require all overloads to return the same type
 

What about generic constructor signatures?

interface I { new<T>() T; }

We would disallow generic constructor signatures.  These can’t arise as the type of a signature of a class’s constructor, so we’d follow a similar restriction.

What are the scoping rules of class B extends class C { /* can use T here? */ } { ... }

Needs answer

There is an ambiguity for parsing these as expressions (less than B, greater than, etc etc)

We would need to tweaking the parsing precedence to ensure the correct parse is checked first.

@sophiajt sophiajt changed the title Class expressions Extending class 'extends' to work with expressions Aug 18, 2014
@fdecampredon
Copy link

what's the difference with #511 ? Does this issue is only for class expression ?

@danquirk
Copy link
Member

Yeah gonna dupe this against #511 since that's the ES6 related work that must be done for this.

@danquirk danquirk added the Duplicate An existing issue was already created label Jan 26, 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
Duplicate An existing issue was already created Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants