microsoft / microsoft/TypeScript

bug: error message says 0n is truthy

Open
#60,320 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Control Flow
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/compiler/checker.ts at the linked predicate-semantics function and reproduce the behavior in the provided TypeScript Playground link. Confirm that 0n is classified as always falsy and that the diagnostic changes from TS 2872 to TS 2873, with coverage added using the compiler’s existing test structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.