Skip to content

Rule proposal: Prefer non-null assertion operator over type assertion #202

@sindresorhus

Description

@sindresorhus

I have the following code:

function foo(count: number) {}

// …

foo(count: lookup);

Where lookup is number | undefined.

In this code path, I know for sure that lookup will be a number, so I have done the following:

foo(count: lookup as number);

But a better solution would have been to use the non-null assertion operator:

foo(count: lookup!);

I would like a rule that enforces this.


It should be noted in the rule docs that this conflicts with the no-non-null-assertion rule.

Alternatively, the no-non-null-assertion could be renamed to non-null-assertion and handle both ways. That might be even better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy