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
{{ message }}
Repository navigation
Regression: ERROR TS2564: Property 'module/A#c' has no initializer and is not assigned in the constructor before 'this' is used or returned. #2576
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)
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.
Activity
dcodeIO commentedon Nov 23, 2022
This is in fact a relatively strict check atm, but it is intended. Imagine that
this.bis a property that executes arbitrary code (accessingthis) when accessed, then in that codethis.chas 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 🤔ppppqp commentedon Jan 28, 2023
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
thisin 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.This compiles just fine.
A smarter way to handle this is to only check the accessed properties rather than all properties on
this. What do you think?