diff --git a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts index bad365fedd..bb2014a842 100644 --- a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts +++ b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts @@ -166,8 +166,14 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor return context.isStatement ? result : ts.createParen(result); } - visitWriteKeyExpr(expr: WriteKeyExpr, context: Context): never { - throw new Error('Method not implemented.'); + visitWriteKeyExpr(expr: WriteKeyExpr, context: Context): ts.Expression { + const exprContext = context.withExpressionMode; + const lhs = ts.createElementAccess( + expr.receiver.visitExpression(this, exprContext), + expr.index.visitExpression(this, exprContext), ); + const rhs = expr.value.visitExpression(this, exprContext); + const result: ts.Expression = ts.createBinary(lhs, ts.SyntaxKind.EqualsToken, rhs); + return context.isStatement ? result : ts.createParen(result); } visitWritePropExpr(expr: WritePropExpr, context: Context): ts.BinaryExpression { diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index 4d4a55098d..bd21e0ffb6 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -863,6 +863,25 @@ describe('ngtsc behavioral tests', () => { expect(jsContents).toMatch(contentQueryRegExp('ViewContainerRef', true)); }); + it('should compile expressions that write keys', () => { + env.tsconfig(); + env.write(`test.ts`, ` + import {Component, ContentChild, TemplateRef, ViewContainerRef, forwardRef} from '@angular/core'; + + @Component({ + selector: 'test', + template: '
', + }) + class TestCmp { + test: any; + key: string; + } + `); + + env.driveMain(); + expect(env.getContents('test.js')).toContain('test[key] = $event'); + }); + it('should generate host listeners for components', () => { env.tsconfig(); env.write(`test.ts`, `