diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts index d1d08d50b0..7297da50f0 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts @@ -194,9 +194,8 @@ class TcbTemplateContextOp extends TcbOp { super(); } - get optional() { - return false; - } + // The declaration of the context variable is only needed when the context is actually referenced. + readonly optional = true; execute(): ts.Identifier { // Allocate a template ctx variable and declare it with an 'any' type. The type of this variable diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts index ed60b9d31f..f81ac38469 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts @@ -763,13 +763,13 @@ describe('type check blocks', () => { it('generates a references var when enabled', () => { const block = tcb(TEMPLATE, DIRECTIVES); - expect(block).toContain('var _t2 = (_t1 as any as core.TemplateRef);'); + expect(block).toContain('var _t1 = (_t2 as any as core.TemplateRef);'); }); it('generates a reference var when disabled', () => { const DISABLED_CONFIG: TypeCheckingConfig = {...BASE_CONFIG, checkTemplateBodies: false}; const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG); - expect(block).toContain('var _t2 = (_t1 as any as core.TemplateRef);'); + expect(block).toContain('var _t1 = (_t2 as any as core.TemplateRef);'); }); }); @@ -913,8 +913,8 @@ describe('type check blocks', () => { it('should trace references to an when enabled', () => { const block = tcb(TEMPLATE, DIRECTIVES); expect(block).toContain( - 'var _t4 = (_t3 as any as core.TemplateRef); ' + - '"" + (((_t4).value2));'); + 'var _t3 = (_t4 as any as core.TemplateRef); ' + + '"" + (((_t3).value2));'); }); it('should use any for reference types when disabled', () => {