Skip to content

fix(compiler): report errors at arrow parameters - #71734

Merged
thePunderWoman merged 3 commits into
angular:mainfrom
Flarette:fix/template-arrow-param-diagnostics
Nov 9, 2026
Merged

thePunderWoman merged 3 commits into
angular:mainfrom
Flarette:fix/template-arrow-param-diagnostics

Conversation

@Flarette

Copy link
Copy Markdown
Contributor

Fixes #71733

Why the change

Under strictTemplates, a TypeScript error that starts at a template arrow function parameter, such as a callback that declares a parameter its caller never passes, is silently dropped; with this fix Angular reports it at the parameter.

Special things to note

  • This PR rewrites the existing spec should ignore diagnostics on arrow function parameters, which asserted the D:ignore marker. The marker's only purpose was hiding implicit any errors, and those stay hidden. See Why the old D:ignore spec changes.
  • The fix depends on correct parameter locations. The parser left the expression's offset out of them, so inside {{…}} they started too early ({(val instead of val, and further off in later interpolations). This PR fixes that too. Event bindings have no offset, so they weren't affected.
  • Newly reported errors also include duplicate parameter names: (a, a) => a now reports TS2300 at both as. Language service: go-to-definition on an arrow parameter reference now returns the parameter (val) instead of the enclosing expression (val + 1).

Change outline

The template from the issue:

<button (click)="retry((attempt) => load(attempt))">Retry</button>

What the type-check block emits for the arrow function. Other location comments are left out:

- (this).retry(attempt /*D:ignore*/ => ...)
+ (this).retry(attempt /*24,31*/ => ...)

How the TS2345 error, which starts at attempt, is mapped back to the template:

 translateDiagnostic(TS2345 at `attempt`)
   shouldReportDiagnostic            → TS2345 isn't filtered by code
   findSourceLocation(`attempt`)
-    has the D:ignore marker         → null → error dropped
+    has a location comment          → reported at `attempt`

Parameter locations from the parser, for {{a}} {{(foo, bar) => foo + bar}}:

- foo → `{a}} {{(foo`    bar → `{{(foo, bar`
+ foo → `foo`            bar → `bar`

Why the old D:ignore spec changes

type_check_block_spec.ts had should ignore diagnostics on arrow function parameters, which expected (a /*D:ignore*/, b /*D:ignore*/) =>. It came from 87a4223 (#66294), the commit that added template arrow functions. The commit message and the PR don't discuss the marker. The one stated reason is the code comment that commit put on it:

Ignore diagnostics on the node to skip diagnostics from noImplicitAny since users aren't able to set types on the parameters. Note that this is preferable to setting their types to any, because it allows us to infer the types when the arrow function is passed as a callback.

Both goals still hold without the marker:

What the marker protected What protects it now Spec, all passing before and after this PR
No implicit any error (TS7006) on parameters that templates can't annotate shouldReportDiagnostic drops TS7006 everywhere in the type-check block. It has done so since 2019 (#33125), before arrow functions existed. Template arrow parameters can only be plain names, so TS7006 is the only implicit any error they can produce. should not report implicit any errors on arrow functions defined in @let (existing, from the same commit, unchanged), should not report implicit any errors on arrow functions without a contextual type (new)
Parameter types are still inferred from the callback, not forced to any Unchanged. Parameters are still emitted without a type; only the trailing comment differs. should infer the types of parameters of arrow functions passed in as callbacks (existing, unchanged), should not report errors on contextually typed arrow function parameters (new)
(Not stated, but also hidden) Unused parameter errors (TS6133, TS6199) shouldReportDiagnostic drops both codes. should not report unused arrow function parameters (new, in typecheck/test/diagnostics_spec.ts, with noUnusedLocals and noUnusedParameters)

The marker also hid every other error that starts at a parameter, which is the bug this PR fixes. The rewritten spec, should add span info to arrow function parameters, now expects (a /*3,4*/, b /*6,7*/) =>.

Changed files:

packages/compiler/src/
├── typecheck/expression.ts           # parameter gets a location comment, not D:ignore
└── expression_parser/parser.ts       # parameter location includes the expression offset
packages/compiler-cli/
├── test/ngtsc/template_typecheck_spec.ts               # 2 specs that fail before, 2 that must stay silent
├── src/ngtsc/typecheck/test/diagnostics_spec.ts        # duplicate names (fails before), unused parameters (stays silent)
└── src/ngtsc/typecheck/test/type_check_block_spec.ts   # expects location comments
packages/compiler/test/expression_parser/parser_spec.ts # parameter locations in a second {{…}}
packages/language-service/test/definitions_spec.ts      # expects `val` instead of `val + 1`

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

A template type error is not reported when TypeScript places it at an arrow function parameter. Examples are a callback with more parameters than its caller passes, a callback that returns a function instead of a value, and duplicate parameter names.

Issue Number: #71733

What is the new behavior?

The error is reported at the parameter. Implicit any and unused-parameter errors on template arrow functions are still not reported.

Does this PR introduce a breaking change?

  • Yes
  • No

Apps that contain such an error will now fail to compile under strictTemplates. The error was always there, but Angular didn't report it.

Other information

The duplicate-name and unused-parameter specs are in typecheck/test/diagnostics_spec.ts rather than the ngtsc suite. Under the preprocessor backend (ngtsc_ngp), the compiled component code is type-checked as well, and its copy of the arrow function produces its own Duplicate identifier and TS6133 errors without a template location. That happens with and without this PR. diagnostics_spec.ts checks only the type-check block, so these specs pass on both backends.

Template arrow functions were added in 21.2.0, so 22.2.x has this bug too. This is a candidate for the patch branch. #52322 is a precedent: it surfaced comparison errors that an ignore marker hid, landed as a plain fix, and was cherry-picked to the release branch.

Template type checking dropped every TypeScript diagnostic that started
at an arrow function parameter. For example, with a component method
`takes(fn: (x: number) => number)`, the template expression
`takes((value) => () => 1)` compiled without the TS2345 error that
TypeScript reports at `value`.

The type-check block marked each arrow function parameter with an
ignore comment to hide implicit `any` errors, which template authors
can't fix by adding a type. The marker hid all diagnostics that start
at the parameter. Implicit `any` and unused-declaration errors are
already filtered by error code, so parameters now carry their source
span instead, and other errors are reported at the parameter.

The parser left the expression offset out of arrow function parameter
spans, so inside interpolations the spans started too early. The spans
are now correct. As a side effect, go-to-definition on a parameter
reference returns the parameter instead of the enclosing expression.

Fixes angular#71733
@pullapprove
pullapprove Bot requested a review from kirjs October 29, 2026 20:40
@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Oct 29, 2026
@ngbot ngbot Bot added this to the Backlog milestone Oct 29, 2026
@JeanMeche JeanMeche added the action: global presubmit The PR is in need of a google3 global presubmit label Oct 29, 2026
@Flarette
Flarette force-pushed the fix/template-arrow-param-diagnostics branch from e0420ba to 8c7f40e Compare October 30, 2026 09:32
@crisbeto

crisbeto commented Nov 4, 2026

Copy link
Copy Markdown
Member

Passing TGP, except for one app that needs to be cleaned up first.

@crisbeto

crisbeto commented Nov 4, 2026

Copy link
Copy Markdown
Member

Internal app is cleaned up, this should be good to go.

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release and removed action: global presubmit The PR is in need of a google3 global presubmit labels Nov 4, 2026
@crisbeto
crisbeto removed the request for review from kirjs November 4, 2026 19:47
@crisbeto crisbeto added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Nov 4, 2026
@crisbeto

crisbeto commented Nov 4, 2026

Copy link
Copy Markdown
Member

Caretaker note: the presubmit is behaving weirdly for some reason. This passes TGP so it should be good to go.

@thePunderWoman
thePunderWoman merged commit 9731822 into angular:main Nov 9, 2026
28 of 30 checks passed
@thePunderWoman

Copy link
Copy Markdown
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

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

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template type checking drops errors that start at an arrow function parameter

4 participants