microsoft / microsoft/TypeScript

bug: error message says 0n is truthy

Offen
#60,320 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Control Flow
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in src/compiler/checker.ts bei der verlinkten Funktion für Predicate-Semantik und bilde das Verhalten im bereitgestellten TypeScript Playground-Link nach. Bestätige, dass 0n als immer falsy klassifiziert wird und dass sich die Diagnose von TS 2872 zu TS 2873 ändert, wobei Tests mithilfe der bestehenden Teststruktur des Compilers hinzugefügt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.