Skip to content

Check super() placement for parameter properties with native class fields - #65420

Open
墨梓柒 (DrSmoothl) wants to merge 1 commit into
microsoft:mainfrom
DrSmoothl:fix/54445-parameter-property-super-check
Open

墨梓柒 (DrSmoothl) wants to merge 1 commit into
microsoft:mainfrom
DrSmoothl:fix/54445-parameter-property-super-check

Conversation

@DrSmoothl

@DrSmoothl 墨梓柒 (DrSmoothl) commented Nov 10, 2026 •

Copy link
Copy Markdown

Fixes #54445.

Related prior work: #54475.

Analysis

In checkConstructorDeclaration, emitStandardClassFields currently gates both the class-field and parameter-property checks. With native class fields, this skips the placement check for super() even though parameter-property initialization still accesses this. A constructor can therefore compile without TS2401 and throw at runtime because that initialization precedes a non-root-level super() call.

Fix

Check parameter properties independently of emitStandardClassFields, retaining the existing guard for class fields. The Boolean structure becomes:

- !emitStandardClassFields && (hasFieldsRequiringChecks || hasParameterProperties)
+ (!emitStandardClassFields && hasFieldsRequiringChecks) || hasParameterProperties

Add seven constructor scenarios across ES2021, ES2022, and ESNext, with useDefineForClassFields both enabled and disabled. They cover conditional, return-expression, and block-nested super() calls, valid root-level calls, ordinary parameters, field initializers, and this access before super(). The last case also verifies the existing TS2376 check now applies to parameter properties with native fields.

Use @noTypesAndSymbols: true to focus the baselines on diagnostics and JavaScript emit. The six configurations produce twelve baseline files.

Validation

The regression test fails with the original checker in the ES2022/ESNext configurations with useDefineForClassFields: true; all six configurations pass with the fix.

  • npx hereby validate --all
  • npx hereby tidy
  • npx hereby check:format

AI assistance

I used OpenAI Codex to investigate this issue and write the tests.

Copilot AI balanced review requested due to automatic review settings November 10, 2026 03:31
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 10, 2026
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change enforcing root-level super() calls for parameter properties.

No open issues 🎉

What changed in this PR

Fixes #54445 by enforcing root-level super() calls for parameter properties even when the compiler emits native class fields. The existing class-field checks remain conditional on downlevel emit.

Changes:

  • Separates parameter-property checks from the native-class-field guard.
  • Adds seven constructor scenarios across six compiler configurations.
  • Adds diagnostic and JavaScript baselines covering invalid placement, valid calls, and access to this before super().
File Description
tsc/​testdata/​tests/​cases/​compiler/​parameterPropertiesRequireRootLevelSuper.ts Adds regression coverage across targets and field modes.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=esnext,usedefineforclassfields=true).js Records ESNext native-field emit.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=esnext,usedefineforclassfields=true).errors.txt Records ESNext native-field diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=esnext,usedefineforclassfields=false).js Records ESNext assignment-based emit.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=esnext,usedefineforclassfields=false).errors.txt Records ESNext assignment-mode diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2022,usedefineforclassfields=true).js Records ES2022 native-field emit.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2022,usedefineforclassfields=true).errors.txt Records ES2022 native-field diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2022,usedefineforclassfields=false).js Records ES2022 assignment-based emit.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2022,usedefineforclassfields=false).errors.txt Records ES2022 assignment-mode diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2021,usedefineforclassfields=true).js Records ES2021 property-definition emit.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2021,usedefineforclassfields=true).errors.txt Records ES2021 property-definition diagnostics.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2021,usedefineforclassfields=false).js Records ES2021 assignment-based emit.
tsc/​testdata/​baselines/​reference/​compiler/​parameterPropertiesRequireRootLevelSuper(target=es2021,usedefineforclassfields=false).errors.txt Records ES2021 assignment-mode diagnostics.
tsc/​internal/​checker/​checker.go Applies parameter-property placement checks independently of field emit mode and clarifies the comment.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

TS2401 does not work with "target": "ESNext"

2 participants