Class fields w/esnext+[[Define]]:no shadow error #36405
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With useDefineForClassFields: true and ESNext target, initializer expressions for property declarations are evaluated in the scope of the class body and are permitted to reference parameters or local variables of the constructor. This is different from classic Typescript behaviour, with useDefineForClassFields: false. There, initialisers of property declarations are evaluated in the scope of the constructor body, so constructor-local symbols shadow outer symbols of the same name.
In other words:
Is fine with ESNext emit because
x
refers unambiguously tovar x = 1
. In non-esnext emit, you'll getNote that when class fields are accepted in the ECMAScript standard, we'll need to replace ESNext with the current ES20xx.
The emit is already correct for ESNext + useDefineForClassFields, so this PR just removes the error in this case. It leaves the error for other cases, because fixing classic emit is non-trivial and low-value.
Fixes #35085