-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed as not planned
Labels
locked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please 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/docsthings to do with maintenance of the repo, and not with code/docstriageWaiting for team members to take a lookWaiting for team members to take a lookwontfixThis will not be worked onThis will not be worked on
Description
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
Labels
locked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please 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/docsthings to do with maintenance of the repo, and not with code/docstriageWaiting for team members to take a lookWaiting for team members to take a lookwontfixThis will not be worked onThis will not be worked on