Skip to content

typeof does not narrow in templates #59215

@kisfairmed

Description

@kisfairmed

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

I am using @if (with the same behavior observed for *ngIf) to check a variable’s type. While the TypeScript linter accepts this type check without issues, the Angular compiler (ng build and ng serve) fails to compile.

However, if I replace the inline type check with a TypeScript type-guarding function, everything works as expected.

I don’t see a clear difference between the two approaches, so I suspect this might be a bug.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-lk71pysu?file=src%2Fmain.ts&view=editor

Please provide the exception or error you saw

✘ [ERROR] NG2: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. [plugin angular-compiler]

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 19.0.5
Node: 22.12.0
Package Manager: pnpm 8.12.1
OS: darwin x64

Angular: 19.0.4
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.1900.5
@angular-devkit/build-angular      19.0.5
@angular-devkit/core               19.0.5
@angular-devkit/schematics         19.0.5
@angular/cdk                       19.0.3
@angular/cli                       19.0.5
@angular/material                  19.0.3
@angular/material-moment-adapter   19.0.3
@schematics/angular                19.0.5
rxjs                               6.6.7
typescript                         5.6.3
zone.js                            0.15.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions