Description
TypeScript Version: 3.8.0-dev.20200123
Search Terms: useDefineForClassFields parameter
Code
// @useDefineForClassFields: true
// @target: esnext
class C {
bar = this.foo.bar;
constructor(private foo: {bar: string}) {}
}
Expected behavior:
Transpiled code works (bar
is defined in the constructor as it's done for the downlevel emit) OR used-before-assignment error on this.foo
in the initializer of bar
.
Actual behavior:
Since the initializer of bar
is run before the assignment of foo
, there's a runtime error.
This code used to work for ages (of course without native class fields). A lot of code that uses dependency injection looks like this.
Related Issues:
I thought this was already discussed somewhere with @RyanCavanaugh and @sandersn, but I couldn't find an issue for that.
Found while looking at #36405. That PR would need an exception if parameter properties are involved IF you want to continue supporting the example code.