Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): narrow NgIf context variables in template type checker #36627

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion goldens/public-api/common/common.d.ts
Expand Up @@ -243,7 +243,7 @@ export declare class NgIf<T = unknown> {
set ngIfThen(templateRef: TemplateRef<NgIfContext<T>> | null);
constructor(_viewContainer: ViewContainerRef, templateRef: TemplateRef<NgIfContext<T>>);
static ngTemplateGuard_ngIf: 'binding';
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any): ctx is NgIfContext<NonNullable<T>>;
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any): ctx is NgIfContext<Exclude<T, false | 0 | '' | null | undefined>>;
}

export declare class NgIfContext<T = unknown> {
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/directives/ng_if.ts
Expand Up @@ -232,7 +232,8 @@ export class NgIf<T = unknown> {
* The presence of this method is a signal to the Ivy template type-check compiler that the
* `NgIf` structural directive renders its template with a specific context type.
*/
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any): ctx is NgIfContext<NonNullable<T>> {
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any):
ctx is NgIfContext<Exclude<T, false|0|''|null|undefined>> {
return true;
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts
Expand Up @@ -71,7 +71,7 @@ export declare class NgIf<T = unknown> {
ngIfThen: TemplateRef<NgIfContext<T>> | null;
constructor(_viewContainer: ViewContainerRef, templateRef: TemplateRef<NgIfContext<T>>);
static ngTemplateGuard_ngIf: 'binding';
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any): ctx is NgIfContext<NonNullable<T>>;
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any): ctx is NgIfContext<Exclude<T, false | 0 | "" | null | undefined>>;
static ɵdir: i0.ɵɵDirectiveDefWithMeta<NgIf<any>, '[ngIf]', never, {'ngIf': 'ngIf'}, {}, never>;
}

Expand Down Expand Up @@ -817,7 +817,7 @@ export declare class AnimationEvent {
template: '<div *ngIf="user; let u">{{u.name}}</div>',
})
class TestCmp {
user: {name: string}|null;
user: {name: string}|null|false;
}

@NgModule({
Expand All @@ -841,7 +841,7 @@ export declare class AnimationEvent {
template: '<div *ngIf="user as u">{{u.name}}</div>',
})
class TestCmp {
user: {name: string}|null;
user: {name: string}|null|false;
}

@NgModule({
Expand Down