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

super in object literal causes error #5441

Closed
mrac opened this issue Oct 28, 2015 · 5 comments
Closed

super in object literal causes error #5441

mrac opened this issue Oct 28, 2015 · 5 comments
Labels
Bug A bug in TypeScript ES6 Relates to the ES6 Spec Fixed A PR has been merged for this issue

Comments

@mrac
Copy link

mrac commented Oct 28, 2015

The following code should log:

original
new

as it does in ES6. Instead in TypeScript 1.6.2 it causes an error:

'super' can only be referenced in a derived class.

Here's the code:

var obj = {
    __proto__: {
        method() {
            console.log('original');
        }
    },
    method() {
        super.method();
        console.log('new');
    }
};

obj.method();
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 28, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2015

As per the ES6/S2015 spec, super is only allowed in classes. In this sample __proto__ is just a regular poverty and is not treated differently by the typesystem. What works is rewriting this as classes, this should be equivalent:

var obj = class extends class {
    method() {
        console.log('original');
    }
}{
    method() {
        super.method();
        console.log('new');
    }
};

new obj().method();

@mrac
Copy link
Author

mrac commented Oct 28, 2015

I didn't decypher the spec document, but the MDN doc says:

The super.prop and super[expr] expressions are valid in any method definition in both classes and object literals.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super

Chrome, Node, Firefox and Babel all call the prototype method here. No error.
So TypeScript is not compatible with ES6 in this case.

@weswigham
Copy link
Member

@mrac seems right - nothing in the super keyword spec indicates that it is only valid within a class, and, in fact, there are even places that call out that you can't user super in places like certain evals and module statement lists. The spec area indicating that any function with a home object can use super would be Function.[[HasSuperBinding]].

@mhegazy
Copy link
Contributor

mhegazy commented Oct 29, 2015

yes. looking at the final spec that seems the case; though i remember it was not at some point.

@mhegazy mhegazy added Bug A bug in TypeScript ES6 Relates to the ES6 Spec and removed Question An issue which isn't directly actionable in code labels Oct 29, 2015
@mhegazy mhegazy added this to the TypeScript 1.8 milestone Dec 8, 2015
@RyanCavanaugh
Copy link
Member

Design meeting resolution: super should be allowed only in ES6 in a shorthand method body in an object literal and is of type any

@vladima vladima added the Fixed A PR has been merged for this issue label Dec 18, 2015
@vladima vladima closed this as completed Dec 18, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript ES6 Relates to the ES6 Spec Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants