From 3ef5ede6d64d4d1173abd742c030e135dde2ac66 Mon Sep 17 00:00:00 2001 From: Rado Kirov Date: Sat, 24 Sep 2016 02:21:59 +0900 Subject: [PATCH] chore(compiler): emit ([] as any[]) instead of purely []. (#11846) In SNC mode `[]` has type of never[], so we cast it to any[] to typecheck correctly see https://github.com/Microsoft/TypeScript/issues/10479. This is temporary workaround, until we fully migrate the framework to TS 2.0 and strictNullChecks. --- .../@angular/compiler/src/output/ts_emitter.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/@angular/compiler/src/output/ts_emitter.ts b/modules/@angular/compiler/src/output/ts_emitter.ts index d2c0aaabc1..d3bf0fc497 100644 --- a/modules/@angular/compiler/src/output/ts_emitter.ts +++ b/modules/@angular/compiler/src/output/ts_emitter.ts @@ -75,6 +75,22 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor super.visitLiteralExpr(ast, ctx, '(null as any)'); } + + // Temporary workaround to support strictNullCheck enabled consumers of ngc emit. + // In SNC mode, [] have the type never[], so we cast here to any[]. + // TODO: narrow the cast to a more explicit type, or use a pattern that does not + // start with [].concat. see https://github.com/angular/angular/pull/11846 + visitLiteralArrayExpr(ast: o.LiteralArrayExpr, ctx: EmitterVisitorContext): any { + if (ast.entries.length === 0) { + ctx.print('('); + } + const result = super.visitLiteralArrayExpr(ast, ctx); + if (ast.entries.length === 0) { + ctx.print(' as any)'); + } + return result; + } + visitExternalExpr(ast: o.ExternalExpr, ctx: EmitterVisitorContext): any { this._visitIdentifier(ast.value, ast.typeParams, ctx); return null;