ngtsc template type checker does not deduce type information from directives like *ngIf
See original GitHub issueš feature request
Relevant Package
This feature request is for @angular/compiler-cli, particularly the ngtsc/typecheck part.
Description
component.ts:
export class AppComponent {
returnsString(): string { return '1'; }
requiresNumber(arg: number): void { }
}
component.html:
<ng-container *ngIf="returnsString() as value">
<!--
Type of 'value' is detected by Angular language service.
A type error
- IS NOT detected by Angular language service
- IS NOT detected by Angular compiler
Compiled successfully...
-->
<button type="button" (click)="requiresNumber(value)"></button>
</ng-container>
With so much talk about Ivyās ability to typecheck Angular templates, I would naturally expect the obvious type error to be discovered here. If not by Angular language service, then at least during compilation.
I have double- and triple-checked this issue with latest Angular RC, āenableIvyā, āfullTemplateTypeCheckā, āstrict: trueā, and āng build --prodā.
Describe the solution youād like
It would be nice if the template type checker could analyze a template as a whole, producing for the template snippet above TypeScript code like the following:
const value = this.returnsString();
if (value)
button.addEventListener('click', () => this.requiresNumber(value));
which obviously would make the type check fail as expected.
Describe alternatives youāve considered
I know no workarounds.
Version information
$ ng version
Angular CLI: 9.0.0-rc.8
Node: 10.16.0
OS: win32 x64
Angular: 9.0.0-rc.8
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.0-rc.8
@angular-devkit/build-angular 0.900.0-rc.8
@angular-devkit/build-optimizer 0.900.0-rc.8
@angular-devkit/build-webpack 0.900.0-rc.8
@angular-devkit/core 9.0.0-rc.8
@angular-devkit/schematics 9.0.0-rc.8
@ngtools/webpack 9.0.0-rc.8
@schematics/angular 9.0.0-rc.8
@schematics/update 0.900.0-rc.8
rxjs 6.5.4
typescript 3.6.4
webpack 4.41.2
Keywords
ivy, template type checker, fullTemplateTypeCheck, ngIf
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)

Top Related StackOverflow Question
@earshinov Indeed, using
strictTemplatesthis level of type inference is indeed fully functional in 9.0.0-rc.7. The code youāre suggesting to generate is more or less exactly what the compiler does indeed generate š Note that this level of inference requiresstrictInputTypesnot to be disabled.Thereās currently still a restriction with
ngIfand theassyntax to declare a variable corresponding with theNgIfContext.ngIfproperty. The template type checker allows a directive to declare a so-called template guard, which results in theif (value)in your suggested code to be generated. This results in type-narrowing ofvalue, but notNgIfContext.ngIf. In your Material example, theassertItemTypeis probably the one that removes the nullability from the type, so youāre probably not suffering from this limitation. Weāre looking into removing this limitation so that it will work correctly.I would also try with
strictTemplatesoption enabled https://twitter.com/yurzui/status/1206787411210055680Demo