diff --git a/modules/@angular/compiler/src/output/abstract_emitter.ts b/modules/@angular/compiler/src/output/abstract_emitter.ts index 1a064fb582..caa218f95e 100644 --- a/modules/@angular/compiler/src/output/abstract_emitter.ts +++ b/modules/@angular/compiler/src/output/abstract_emitter.ts @@ -250,12 +250,13 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex ctx.print(`)`); return null; } - visitLiteralExpr(ast: o.LiteralExpr, ctx: EmitterVisitorContext): any { + visitLiteralExpr(ast: o.LiteralExpr, ctx: EmitterVisitorContext, absentValue: string = 'null'): + any { var value = ast.value; if (isString(value)) { ctx.print(escapeSingleQuoteString(value, this._escapeDollarInStrings)); } else if (isBlank(value)) { - ctx.print('null'); + ctx.print(absentValue); } else { ctx.print(`${value}`); } diff --git a/modules/@angular/compiler/src/output/ts_emitter.ts b/modules/@angular/compiler/src/output/ts_emitter.ts index d4d4e259f0..b95114bffe 100644 --- a/modules/@angular/compiler/src/output/ts_emitter.ts +++ b/modules/@angular/compiler/src/output/ts_emitter.ts @@ -72,6 +72,10 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor } } + visitLiteralExpr(ast: o.LiteralExpr, ctx: EmitterVisitorContext): any { + super.visitLiteralExpr(ast, ctx, '(null as any)'); + } + visitExternalExpr(ast: o.ExternalExpr, ctx: EmitterVisitorContext): any { this._visitIdentifier(ast.value, ast.typeParams, ctx); return null; diff --git a/modules/@angular/compiler/test/output/ts_emitter_spec.ts b/modules/@angular/compiler/test/output/ts_emitter_spec.ts index e9fa32e695..1799af86d8 100644 --- a/modules/@angular/compiler/test/output/ts_emitter_spec.ts +++ b/modules/@angular/compiler/test/output/ts_emitter_spec.ts @@ -290,35 +290,41 @@ export function main() { it('should support builtin types', () => { var writeVarExpr = o.variable('a').set(o.NULL_EXPR); - expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE))).toEqual('var a:any = null;'); - expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE))).toEqual('var a:boolean = null;'); - expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE))).toEqual('var a:number = null;'); - expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE))).toEqual('var a:number = null;'); - expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE))).toEqual('var a:string = null;'); - expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE))).toEqual('var a:Function = null;'); + expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE))) + .toEqual('var a:any = (null as any);'); + expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE))) + .toEqual('var a:boolean = (null as any);'); + expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE))) + .toEqual('var a:number = (null as any);'); + expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE))) + .toEqual('var a:number = (null as any);'); + expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE))) + .toEqual('var a:string = (null as any);'); + expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE))) + .toEqual('var a:Function = (null as any);'); }); it('should support external types', () => { var writeVarExpr = o.variable('a').set(o.NULL_EXPR); expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier)))) - .toEqual('var a:someLocalId = null;'); + .toEqual('var a:someLocalId = (null as any);'); expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([ `import * as import0 from 'somePackage/someOtherPath';`, - `var a:import0.someExternalId = null;` + `var a:import0.someExternalId = (null as any);` ].join('\n')); }); it('should support combined types', () => { var writeVarExpr = o.variable('a').set(o.NULL_EXPR); expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null)))) - .toEqual('var a:any[] = null;'); + .toEqual('var a:any[] = (null as any);'); expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE)))) - .toEqual('var a:number[] = null;'); + .toEqual('var a:number[] = (null as any);'); expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null)))) - .toEqual('var a:{[key: string]:any} = null;'); + .toEqual('var a:{[key: string]:any} = (null as any);'); expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE)))) - .toEqual('var a:{[key: string]:number} = null;'); + .toEqual('var a:{[key: string]:number} = (null as any);'); }); }); }