No compile error thrown when `this` referenced before call to `super` completes
See original GitHub issueTypeScript Version:
1.8.9
Code JSFiddle: https://jsfiddle.net/kitsonk/fs9t96ep/ TS Playground: http://goo.gl/X7cgvV
class A {
constructor(fn: () => void) {
fn.call(this);
}
foo: string = 'foo';
}
class B extends A {
constructor() {
super(() => {
console.log(this);
});
}
bar: string = 'bar';
}
const b = new B();
Expected behavior:
Typescript should guard against the use of this
as the call to super
has not completed. Thus it should not compile.
Actual behavior:
Typescript compiles this successfully. It works fine when the target is set to es5
but breaks the browser when target is set to es6
.
Error is: VM89:55 Uncaught ReferenceError: this is not defined
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (11 by maintainers)
Top Results From Across the Web
Always allow code before super call when it does not use ...
The error message explains what went wrong precisely I think. This is fine: class MyClass { constructor(public str: string) { } } class ......
Read more >Why do this() and super() have to be the first statement in a ...
(2) The compiler appears to implement a different check which is, by itself, sufficient to prevent this problem. The message is "cannot reference...
Read more >Bug Patterns
AndroidInjection.inject() should always be invoked before calling super.lifecycleMethod(). ArrayEquals. Reference equality used to compare arrays.
Read more >compile time error messages : Java Glossary
You are using a method of the object when the reference is to an interface that does not contain that method. Either cast...
Read more >Exceptions and Error Handling, C++ FAQ
What does throw; (without an exception object after the throw keyword) mean? ... throw is to return a return code (sometimes called an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
One more case (https://gist.github.com/zemlanin/a306e8498d05c923cac405c047014029):
Just a note on this, the emit in master (TS 2.1) now breaks these examples in ES5 at run-time, so this now has parity. It still does not warn you that it is invalid.
Given:
The emit is…
Version 2.1.0-dev.20161103
Version 2.0.6