Closed
Description
π Search Terms
- ts2729
- used before its initialization
- arrow function in fields
- useDefineForClassFields
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about >= 5
β― Playground Link
π» Code
class Foo {
public fieldA = [].map(() => {
// access service inside arrow function shouldn't be work with useDefineForClassFields = true
this.service.do()
// ^^^^^^^ expected error ts(2729)
})
public fieldB = this.service.do()
// ^^^^^^^ Property 'service' is used before its initialization. (ts(2729))
public constructor(private service: { do: () => string }) {}
}
Just for sake of completeness:
JavaScript output with useDefineForClassFields = true
class Foo {
service;
fieldA = [].map(() => {
this.service.do();
});
fieldB = this.service.do();
constructor(service) {
this.service = service;
}
}
JavaScript output with useDefineForClassFields = false
class Foo {
constructor(service) {
this.service = service;
this.fieldA = [].map(() => {
this.service.do();
});
this.fieldB = this.service.do();
}
}
π Actual behavior
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 target
s, everything works. However, I would like to avoid using the old format for classes.
Metadata
Metadata
Assignees
Labels
No labels