You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classB{}classA{b: B;c: B;constructor(){// this should be safe?this.b=newB();this.c=this.b;}}exportfunctionfib(n: i32): i32{leta=newA();return0;}
Error:
;; ERROR TS2564: Property 'module/A#c' has no initializer and is not assigned in the constructor before 'this' is used or returned.
;; :
;; 4 │ c: B;
;; │ ~
;; └─ in module.ts(4,3)
;; :
;; 7 │ this.c = this.b;
;; │ ~~~~~~
;; └─ in module.ts(7,14)
The text was updated successfully, but these errors were encountered:
This is in fact a relatively strict check atm, but it is intended. Imagine that this.b is a property that executes arbitrary code (accessing this) when accessed, then in that code this.c has not yet been assigned.
Edit: Makes me realize that this.b = ... before is similar, but doesn't diagnose an error. I guess what we need there is to detect if something is an actual getter or setter, and only then diagnose 🤔
I was tracking this bug for some time and realized it is actually a feature #1349. In brief, the compiler checks if the class is fully initialized before handling a call on this in the constructor. It is indeed a different behavior than Typescript (so a bit confusing), but as mentioned in the above PR, it can be overcome with an assertion.
Link: here
Minimal reproduction: (as far as I can reduce it)
The text was updated successfully, but these errors were encountered: