diff --git a/packages/compiler-cli/test/diagnostics/check_types_spec.ts b/packages/compiler-cli/test/diagnostics/check_types_spec.ts index 9a3dc9439e..18401a18a7 100644 --- a/packages/compiler-cli/test/diagnostics/check_types_spec.ts +++ b/packages/compiler-cli/test/diagnostics/check_types_spec.ts @@ -87,6 +87,23 @@ describe('ng type checker', () => { addTests({fullTemplateTypeCheck: true}); }); + describe('regressions', () => { + // #19485 + it('should accept if else (TemplateRef)', () => { + accept( + { + 'src/app.component.html': ` +
+ No person supplied. +
+ + Welcome {{person.name}}! + ` + }, + {fullTemplateTypeCheck: true}); + }); + }); + function addTests(config: {fullTemplateTypeCheck: boolean}) { function a(template: string) { accept({'src/app.component.html': template}, config); } @@ -234,6 +251,7 @@ const QUICKSTART = { `, 'src/app.module.ts': ` import { NgModule } from '@angular/core'; + import { CommonModule } from '@angular/common'; import { AppComponent, APipe, ADirective } from './app.component'; import { LibDirective, LibPipe } from './lib'; @@ -246,7 +264,7 @@ const QUICKSTART = { @NgModule({ declarations: [ AppComponent, APipe, ADirective ], bootstrap: [ AppComponent ], - imports: [ LibModule ] + imports: [ LibModule, CommonModule ] }) export class AppModule { } ` diff --git a/packages/compiler/src/aot/compiler.ts b/packages/compiler/src/aot/compiler.ts index 07b5ccc517..e8c80d182d 100644 --- a/packages/compiler/src/aot/compiler.ts +++ b/packages/compiler/src/aot/compiler.ts @@ -206,11 +206,16 @@ export class AotCompiler { // and they also cause TypeScript to include these files into the program too, // which will make them part of the analyzedFiles. const externalReferences: StaticSymbol[] = [ + // Add references that are available from all the modules and imports. ...ngModuleMeta.transitiveModule.directives.map(d => d.reference), ...ngModuleMeta.transitiveModule.pipes.map(d => d.reference), ...ngModuleMeta.importedModules.map(m => m.type.reference), ...ngModuleMeta.exportedModules.map(m => m.type.reference), + + // Add references that might be inserted by the template compiler. + ...this._externalIdentifierReferences([Identifiers.TemplateRef, Identifiers.ElementRef]), ]; + const externalReferenceVars = new Map(); externalReferences.forEach((ref, typeIndex) => { if (this._host.isSourceFile(ref.filePath)) { @@ -248,6 +253,17 @@ export class AotCompiler { } } + private _externalIdentifierReferences(references: o.ExternalReference[]): StaticSymbol[] { + const result: StaticSymbol[] = []; + for (let reference of references) { + const token = createTokenForExternalReference(this._reflector, reference); + if (token.identifier) { + result.push(token.identifier.reference); + } + } + return result; + } + private _createTypeCheckBlock( ctx: OutputContext, componentId: string, moduleMeta: CompileNgModuleMetadata, compMeta: CompileDirectiveMetadata, directives: CompileIdentifierMetadata[],