Skip to content

Repo: Convert nullThrows to an assertion function #10077

Closed as not planned
Closed as not planned
@kirkwaiblinger

Description

@kirkwaiblinger

Suggestion

Right now our nullThrows utility has signature like so:

declare function nullThrows<T>(x: T, reason: string): NonNullable<T>;

which basically models a runtime non-null assertion operator x!.

However, unlike the non-null assertion operator, we don't have a way to flag when the argument provided is already non-null

const nonNull = 'foo';
const foo1 = nonNull!; // violates no-unnecessary-type-assertion
const foo2 = nullThrows(foo1, 'reason'); // no linter report.

We could pretty easily right our own custom rule in order to flag this scenario, however, now that #10009 has landed, I propose let's change the signature to be an assertion function so that we can dogfood no-unnecessary-condition's checkTypePredicates functionality.

declare function nullThrows<T>(x: T, reason: string): asserts x is NonNullable<T>;

const nonNull = 'foo';

nullThrows(nonNull, 'reason'); // no-unnecessary-condition - types are already equal

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.repo maintenancethings to do with maintenance of the repo, and not with code/docstriageWaiting for team members to take a lookwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions