Skip to content

Enhancement: [strict-boolean-expressions] Check truthiness assertion functions #9009

Closed
@alecmev

Description

@alecmev

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/strict-boolean-expressions/

Description

For example, ok from node:assert has the following shape:

function ok(value: unknown, message?: string | Error): asserts value;

It appears that currently the rule just looks at value, sees unknown, and moves on. So it's possible to write this:

const foo: string | undefined = 'bar';
if (foo) ... // Error
ok(foo); // Fine

Instead, it would be nice if it also looked at the return type and took asserts value into account, treating it as if (!value) throw. There's more to asserts than just checking truthiness, but handling the simplest case (no is) would already go a long way, as that covers all ok-like assertion functions out there.

Fail

const foo: string | undefined = 'bar';
ok(foo);

Pass

const foo: string | undefined = 'bar';
ok(foo !== undefined);

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions