Skip to content

bug: error message says 0n is truthyΒ #60320

Description

πŸ”Ž Search Terms

falsey, truthy, bigint, PseudoBigInt, TS Error 2872, TS Error 2873, unnecessary condition, constant condition

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=5.7.0-dev.20241022#code/JYMwBAFADAdglGA3mFAoAvkA

πŸ’» Code

0n is falsy.

if (0n) {
  // won't happen
}

πŸ™ Actual behavior

TS 2872: This kind of expression is always truthy.

πŸ™‚ Expected behavior

TS 2873: This kind of expression is always falsy.

Additional information about the issue

I am presuming that this is due to this function, which always reports BigIntLiteral as PredicateSemantics.Always, rather than checking for 0n.

https://github.com/microsoft/TypeScript/blob/c07da583afbfbb68930448b781832c74b3f713e6/src/compiler/checker.ts#L44498-L44532

Looks like this has been present since the always-truthy/always-falsey check was initially created: https://github.com/microsoft/TypeScript/pull/59217/files#diff-d9ab6589e714c71e657f601cf30ff51dfc607fc98419bf72e04f6b0fa92cc4b8R44293

I'd be happy to drop a PR if this is accepted and that's the issue.

Activity

  1. kirkwaiblinger commented on Oct 22, 2024

    @kirkwaiblinger
    Author

    While poking around at this, I noticed that the checks also lack support for negative numbers (-0, -0n, -123, -123n) - I've put this all into a draft PR if you're interested, see #60324

    EDIT - also explicitly positive numbers, like +123

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

    BugA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysis

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions