Skip to content

Warn on non-nullable optional chain #44870

Description

@dgp1130

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

compiler-cli

Description

Optional chaining is a great way to safely access properties that might be null or undefined, for example:

import {Component, Input} from '@angular/core';

@Component({
  template: `
    <span>Hello, {{ user?.name ?? 'guest' }}!</span>
  `,
})
export class Greeter {
  @Input() user?: User;
}

interface User {
  name: string;
}

But things can easily change and make the optional chain useless, such as:

  export class Greeter {
-   @Input() user?: User;
+   @Input() user: User;
  }

This change means that the optional chain user?.name is no longer useful because user will never be null or undefined. This leads to confusion and tricks readers of the code into thinking that there is a case where "Hello, guest!" is displayed, when that actually isn't the case. If the default "guest" string comes from somewhere else, it could lead to dead code and possibly result in excess bundle size.

Proposed solution

We should consider an extended diagnostic to identify and warn developers in this situation. We already have a diagnostic to warn about useless nullish coalescing operators ??, so it makes sense to have a similar check for optional chaining.

Alternatives considered

N/A

Activity

  1. added
    featureLabel used to distinguish feature request from other issues
    area: compilerIssues related to `ngc`, Angular's template compiler
    on Jan 28, 2022
  2. modified the milestone: Backlog on Jan 28, 2022
  3. angular-robot commented on Jan 28, 2022

    @angular-robot
    Contributor

    This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

    You can find more details about the feature request process in our documentation.

  4. angular-robot commented on Mar 8, 2022

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  5. angular-robot commented on Mar 28, 2022

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  6. angular-automatic-lock-bot commented on Aug 12, 2022

    @angular-automatic-lock-bot

    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.

  7. locked and limited conversation to collaborators on Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions