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
classFoo{publicfieldA=[].map(()=>{// access service inside arrow function shouldn't be work with useDefineForClassFields = truethis.service.do()// ^^^^^^^ expected error ts(2729)})publicfieldB=this.service.do()// ^^^^^^^ Property 'service' is used before its initialization. (ts(2729))publicconstructor(privateservice: {do: ()=>string}){}}
Just for sake of completeness:
JavaScript output with useDefineForClassFields = true
Error Property 'service' is used before its initialization.(2729) only occurs for fieldB initialization.
π Expected behavior
Error Property 'service' is used before its initialization.(2729) should also occur for fieldA initialization inside the arrow function.
Additional information about the issue
With useDefineForClassFields = true (which is the default) the generated JavaScript code in my example won't work, because service is accessed even though it is still undefined.
If I set the setting to false, as is done automatically for earlier targets, everything works. However, I would like to avoid using the old format for classes.
The text was updated successfully, but these errors were encountered:
This is working as intended. The compiler doesn't know when your arrow function is invoked, and it's a reasonable assumption it will be invoked after your variable is set.
This would be improved with #58729, allowing the compiler to know that the arrow function passed to Array.map() will be invoked immediately.
π Search Terms
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=11#code/MYGwhgzhAEBiD29oG8BQ1oAcCuAjEAlsNAGYECmIAJgILQC80A2gLoB0AtmJgBQ8CUDAHwp0GaABcAFgQhsI5AE4A3IuTZV4AsQF9+qMTnxFSFagCEGkmXIUq1GrfsN5CxYPAB2ECYuzAJeEUeTEUCZTAJcmg7VWByAC4UaE0kgWEY3wJPAHNoPRQdVB0gA
π» Code
Just for sake of completeness:
JavaScript output with useDefineForClassFields = true
JavaScript output with useDefineForClassFields = false
π Actual behavior
Error
Property 'service' is used before its initialization.(2729)
only occurs forfieldB
initialization.π Expected behavior
Error
Property 'service' is used before its initialization.(2729)
should also occur forfieldA
initialization inside the arrow function.Additional information about the issue
With
useDefineForClassFields = true
(which is the default) the generated JavaScript code in my example won't work, becauseservice
is accessed even though it is stillundefined
.If I set the setting to
false
, as is done automatically for earliertarget
s, everything works. However, I would like to avoid using the old format for classes.The text was updated successfully, but these errors were encountered: