@@ -149,22 +149,22 @@ import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef, ɵstri
149149 * @publicApi
150150 */
151151@Directive ( { selector : '[ngIf]' } )
152- export class NgIf {
153- private _context : NgIfContext = new NgIfContext ( ) ;
154- private _thenTemplateRef : TemplateRef < NgIfContext > | null = null ;
155- private _elseTemplateRef : TemplateRef < NgIfContext > | null = null ;
156- private _thenViewRef : EmbeddedViewRef < NgIfContext > | null = null ;
157- private _elseViewRef : EmbeddedViewRef < NgIfContext > | null = null ;
152+ export class NgIf < T > {
153+ private _context : NgIfContext < T > = new NgIfContext < T > ( ) ;
154+ private _thenTemplateRef : TemplateRef < NgIfContext < T > > | null = null ;
155+ private _elseTemplateRef : TemplateRef < NgIfContext < T > > | null = null ;
156+ private _thenViewRef : EmbeddedViewRef < NgIfContext < T > > | null = null ;
157+ private _elseViewRef : EmbeddedViewRef < NgIfContext < T > > | null = null ;
158158
159- constructor ( private _viewContainer : ViewContainerRef , templateRef : TemplateRef < NgIfContext > ) {
159+ constructor ( private _viewContainer : ViewContainerRef , templateRef : TemplateRef < NgIfContext < T > > ) {
160160 this . _thenTemplateRef = templateRef ;
161161 }
162162
163163 /**
164164 * The Boolean expression to evaluate as the condition for showing a template.
165165 */
166166 @Input ( )
167- set ngIf ( condition : any ) {
167+ set ngIf ( condition : T ) {
168168 this . _context . $implicit = this . _context . ngIf = condition ;
169169 this . _updateView ( ) ;
170170 }
@@ -173,7 +173,7 @@ export class NgIf {
173173 * A template to show if the condition expression evaluates to true.
174174 */
175175 @Input ( )
176- set ngIfThen ( templateRef : TemplateRef < NgIfContext > | null ) {
176+ set ngIfThen ( templateRef : TemplateRef < NgIfContext < T > > | null ) {
177177 assertTemplate ( 'ngIfThen' , templateRef ) ;
178178 this . _thenTemplateRef = templateRef ;
179179 this . _thenViewRef = null ; // clear previous view if any.
@@ -184,7 +184,7 @@ export class NgIf {
184184 * A template to show if the condition expression evaluates to false.
185185 */
186186 @Input ( )
187- set ngIfElse ( templateRef : TemplateRef < NgIfContext > | null ) {
187+ set ngIfElse ( templateRef : TemplateRef < NgIfContext < T > > | null ) {
188188 assertTemplate ( 'ngIfElse' , templateRef ) ;
189189 this . _elseTemplateRef = templateRef ;
190190 this . _elseViewRef = null ; // clear previous view if any.
@@ -225,14 +225,22 @@ export class NgIf {
225225 * narrow its type, which allows the strictNullChecks feature of TypeScript to work with `NgIf`.
226226 */
227227 static ngTemplateGuard_ngIf : 'binding' ;
228+
229+ /**
230+ * Asserts the correct type of the context for the template that `NgIf` will render.
231+ *
232+ * The presence of this method is a signal to the Ivy template type-check compiler that the
233+ * `NgIf` structural directive renders its template with a specific context type.
234+ */
235+ static ngTemplateContextGuard < T > ( dir : NgIf < T > , ctx : any ) : ctx is NgIfContext < T > { return true ; }
228236}
229237
230238/**
231239 * @publicApi
232240 */
233- export class NgIfContext {
234- public $implicit : any = null ;
235- public ngIf : any = null ;
241+ export class NgIfContext < T > {
242+ public $implicit : T = null ! ;
243+ public ngIf : T = null ! ;
236244}
237245
238246function assertTemplate ( property : string , templateRef : TemplateRef < any > | null ) : void {
0 commit comments