Skip to content

useDefineForClassFields and parameter properties: missing error or bad emitΒ #36410

Closed
@ajafff

Description

@ajafff

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.

Playground Link: https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=99&ts=3.8.0-dev.20200123&ssl=4&ssc=2&pln=1&pc=1#code/MYGwhgzhAEDC0G8BQ1oCMwCdoF5oBcALASwgDoAzAeyrI0wG4VpgqA7CfTAV2HyswAKAA6ZiANzD4AptGpUAXInpLOYtgHMAvgEpEWpFqA

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions