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 call of non-method incorrectly allowed if target >= ES6 #32121
Comments
What's the expected outcome at runtime? Since the property is not on the prototype there's no way to access its value in the superclass after it's overridden. |
I don’t understand how this can work anyway, since arrow functions don’t have their own |
The mismatch between the title of the issue and the Expected / Actual behaviors it describes is confusing. The case needs to be (detected and) handled. Now there's a choice to make between: A. Making As long as Supporting the behavior could be done by:
N.B: if Here's what the transpiled code would like -- (B1): "use strict";
class Foo {
constructor() {
this.bar = () => { };
}
}
class SubFoo extends Foo {
constructor() {
super(...arguments);
const _ts_bar = this.bar //
this.bar = () => { _ts_bar() } // this.bar = () => { super.bar(); };
}
}
new SubFoo().bar(); Note: the comments show how things currently are. Given that the above solution requires an extra variable (extra code :-S) (and that I haven't thought about a way to make it work if Here's what the solution might look like in TypeScript -- (C): class Foo {
public bar = () => { }
}
class SubFoo extends Foo {
private __super_bar = this.bar
public bar = () => { this.__super_bar(); }
}
new SubFoo().bar() -> In the TypeScript Playgrond solution Note though that it is extra dirty because what was a local variable in (B.) is now a property on the instance. Typescript places the declarations of the body of the class at the beginning of the constructor, so the code can't be moved to the constructor -- the method is already redefined. Here is a Typescript Playground link for the issue by the way: |
Interestingly, this is only a problem with arrow function derived and arrow function base. class Fruit {
public eat() { console.log('nom nom') }
public toss = () => { console.log('nyah!') }
public drop() { console.log('huh?') }
public digest = () => { console.log('burp!') }
}
class Apple extends Fruit {
public eat() { super.eat() } // ok
public toss = () => { super.toss() } // runtime error
public drop = () => { super.drop() } // ok
// public digest() { super.burp() } // compile error
}
new Apple().eat() // ok
new Apple().drop() // ok
new Apple().toss() // super.toss is not a function |
To correct the knowledge model - it is class properties in general that are the problem, not just arrow functions. Method definitions are declared on the prototype. I.e. in your example However properties are declared on the instance, not the prototype. This is because class properties are essentially specced to be syntactic sugar: class Foo { bar = 1 }
// is exactly the same as
class Foo { constructor() { this.bar = 1 } } In essence, when you use I came here as I discovered the same bug in flow and was curious if TS had the same limitation. |
From https://stackoverflow.com/questions/56775734/error-when-calling-super-method-in-a-typescript-create-react-app-project
TypeScript Version: 3.6.0-dev.20190624
Search Terms:
Code
With
target: "es5"
Expected behavior:
No errors. Call to
super
is allowed.Actual behavior:
See error on
super.bar()
Everything works fine using normal (non-arrow) methods. It also works with
"target": "es6"
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: