Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Signal initial null value and template null-check cause unnecessary type errors despite @if guard #62181

Closed as duplicate of#49161
@ferhado

Description

@ferhado

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

core

Is this a regression?

No

Description

When using signals with a type that requires an initial value, I must set it to null or undefined (e.g., signal<User | null>(null)). In the template, I guard access with @if (currentUser()) { ... }. However, it still reports a type error when accessing properties like currentUser().name unless I use the safe navigation operator ?..

This means the null check in @if is ignored for type narrowing.

Example:

currentUser = signal<User>();

It requires an initial value. Since you don’t have a user yet, you must initialize it with null or undefined:

currentUser = signal<User | null>(null);

In the template, when you try:

@if (currentUser()) { <!-- Ignored -->
  <p>{{ currentUser().name }}</p> <!-- Type error -->
}

The @if directive is ignored for type narrowing, so you get a type error on currentUser().name. You have to write it like this:

@if (currentUser()) {
  <p>{{ currentUser()?.name }}</p>
}

This works and is acceptable, but it feels verbose and redundant.

Now, when you have a dumb component where the user input is required:

user = input.required<User>();

You are sure the user will always be provided, so you want to avoid unnecessary null checks. But in the template:

@if (currentUser()) { <!-- Ignored -->
  <show-user [user]="currentUser()" /> <!-- Type error -->
}

You still get a type error because the @if directive does not narrow the type properly. To avoid this, you have to accept null in the input as well:

user = input.required<User | null>();

This forces you to write many unnecessary null checks even when you are sure the data is present.

The root cause is that the @if directive ignores type narrowing on signals, leading to awkward template code and type errors even where data is guaranteed to be non-null.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

X [ERROR] NG1: Object is possibly 'null'

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

Angular CLI: 20.0.3
Node: 22.13.1
Package Manager: npm 11.4.1
OS: win32 x64

Angular: 20.0.4
... common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2000.3
@angular-devkit/core         20.0.3
@angular-devkit/schematics   20.0.3
@angular/build               20.0.3
@angular/cdk                 20.0.3
@angular/cli                 20.0.3
@angular/material            20.0.3
@schematics/angular          20.0.3
rxjs                         7.8.2
typescript                   5.8.3

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions