8000 Improve template null check for used signals in templates · Issue #56303 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

Improve template null check for used signals in templates #56303

New issue

Have a question about this project? Sign u 8000 p 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

Closed
styriandev opened this issue Jun 6, 2024 · 3 comments
Closed

Improve template null check for used signals in templates #56303

styriandev opened this issue Jun 6, 2024 · 3 comments

Comments

@styriandev
Copy link

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

When using a signal in the template, it would be beneficial if the null check is considered for the accessor, reducing the need for repetitive null checks when accessing properties of the object.

Use Case:
Consider the following scenario in an Angular component where a Person object is accessed:

import { Component, signal } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

interface Person {
  name: string;
}
@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h1>Hello from {{ name }}!</h1>
    <div>
      @if(person()) {
        {{ person().name }}
      }
    </div>
  `,
})
export class App {
  name = 'Angular';
  person = signal<null | Person>(null);


}

bootstrapApplication(App);

Right now the @if expression already checks if the person is defined. Therefore I would expect that the person().name expression just works but it is still considered as possibly null. Of course, you could handle this using an @if (person() as personDefined) expression but there are some use cases where it would be easier to just check before if a variable is defined and then access the variable.

The error is the following:
image

Example URL: https://stackblitz.com/edit/stackblitz-starters-zs2wwy?file=src%2Fmain.ts

Proposed solution

If a signal is checked against null it is not considered as potentially null inside the if statement.

Alternatives considered

Probably a structural let directive or something like that would also help in this matter. But in the end a typesafe check should already be enough.

@styriandev styriandev changed the title Improve Template Null Check for used signals in templates Improve template null check for used signals in templates Jun 6, 2024
@JoostK
Copy link
Member
JoostK commented Jun 6, 2024

We're aware of this and this is being tracked in #49161

@JoostK JoostK closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2024
@JoostK
Copy link
Member
JoostK commented Jun 6, 2024

Probably a structural let directive or something like that would also help in this matter.

This is also coming in the form of @let (#15280)

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0