Skip to content

Commit 45b030b

Browse files
committed
fix(compiler-cli): prevent dom event assertion in TCB generation on older angular versions (#63053)
This fixes an issue caused by #62648 for older versions of Angular when the newest version of the language service is used. This prevents the TCB from attempting to use the assertType when it does not exist. fixes #63046 PR Close #63053
1 parent 0d12eac commit 45b030b

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

packages/compiler-cli/src/ngtsc/core/src/compiler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,9 @@ export class NgCompiler {
10571057
const allowSignalsInTwoWayBindings =
10581058
this.angularCoreVersion === null ||
10591059
coreVersionSupportsFeature(this.angularCoreVersion, '>= 17.2.0-0');
1060+
const allowDomEventAssertion =
1061+
this.angularCoreVersion === null ||
1062+
coreVersionSupportsFeature(this.angularCoreVersion, '>= 20.2.0');
10601063

10611064
// First select a type-checking configuration, based on whether full template type-checking is
10621065
// requested.
@@ -1101,6 +1104,7 @@ export class NgCompiler {
11011104
this.options.extendedDiagnostics?.defaultCategory || DiagnosticCategoryLabel.Warning,
11021105
allowSignalsInTwoWayBindings,
11031106
checkTwoWayBoundEvents,
1107+
allowDomEventAssertion,
11041108
};
11051109
} else {
11061110
typeCheckingConfig = {
@@ -1136,6 +1140,7 @@ export class NgCompiler {
11361140
this.options.extendedDiagnostics?.defaultCategory || DiagnosticCategoryLabel.Warning,
11371141
allowSignalsInTwoWayBindings,
11381142
checkTwoWayBoundEvents,
1143+
allowDomEventAssertion,
11391144
};
11401145
}
11411146

packages/compiler-cli/src/ngtsc/typecheck/api/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ export interface TypeCheckingConfig {
353353
*/
354354
allowSignalsInTwoWayBindings: boolean;
355355

356+
/**
357+
* Whether the type of DOM events should be asserted with '@angular/core' 'ɵassertType' (see TCB implementation).
358+
*/
359+
allowDomEventAssertion: boolean;
360+
356361
/**
357362
* Whether to descend into the bodies of control flow blocks (`@if`, `@switch` and `@for`).
358363
*/

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,8 @@ class TcbUnclaimedOutputsOp extends TcbOp {
16801680
if (
16811681
this.target instanceof TmplAstElement &&
16821682
this.target.isVoid &&
1683-
ts.isIdentifier(target)
1683+
ts.isIdentifier(target) &&
1684+
this.tcb.env.config.allowDomEventAssertion
16841685
) {
16851686
domEventAssertion = ts.factory.createCallExpression(
16861687
this.tcb.env.referenceExternalSymbol('@angular/core', 'ɵassertType'),

packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ describe('type check blocks', () => {
10771077
unusedStandaloneImports: 'warning',
10781078
allowSignalsInTwoWayBindings: true,
10791079
checkTwoWayBoundEvents: true,
1080+
allowDomEventAssertion: true,
10801081
};
10811082

10821083
describe('config.applyTemplateContextGuards', () => {
@@ -1455,6 +1456,14 @@ describe('type check blocks', () => {
14551456
});
14561457
});
14571458

1459+
it('should _not_ assert the type for DOM events bound on void elements when disabled', () => {
1460+
const result = tcb(`<input (input)="handleInput($event.target.value)">`, undefined, {
1461+
...BASE_CONFIG,
1462+
allowDomEventAssertion: false,
1463+
});
1464+
expect(result).not.toContain('ɵassertType');
1465+
});
1466+
14581467
describe('config.allowSignalsInTwoWayBindings', () => {
14591468
it('should not unwrap signals in two-way binding expressions', () => {
14601469
const TEMPLATE = `<div twoWay [(input)]="value"></div>`;

packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export const ALL_ENABLED_CONFIG: Readonly<TypeCheckingConfig> = {
291291
unusedStandaloneImports: 'warning',
292292
allowSignalsInTwoWayBindings: true,
293293
checkTwoWayBoundEvents: true,
294+
allowDomEventAssertion: true,
294295
};
295296

296297
// Remove 'ref' from TypeCheckableDirectiveMeta and add a 'selector' instead.
@@ -433,6 +434,7 @@ export function tcb(
433434
suggestionsForSuboptimalTypeInference: false,
434435
allowSignalsInTwoWayBindings: true,
435436
checkTwoWayBoundEvents: true,
437+
allowDomEventAssertion: true,
436438
...config,
437439
};
438440
options = options || {emitSpans: false};

0 commit comments

Comments
 (0)