Fix -NaN emission for enum members in declaration output#3931
Open
Copilot wants to merge 2 commits into
Open
Conversation
Handle NaN and Infinity special values in the declaration transformer and CreateLiteralConstValue, matching the behavior of constantExpression and the const enum inliner. Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/601227f3-8ab0-43b0-ac60-67c51aa20b12 Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix tsgo emitting -NaN for enum members
Fix -NaN emission for enum members in declaration output
May 16, 2026
jakebailey
approved these changes
May 16, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes incorrect -NaN emission in declaration output for enum members. Since NaN >= 0 is false in IEEE 754, NaN values fell into the negative branch and were emitted as -NaN. The fix adds explicit IsInf()/IsNaN() checks before the sign-based branch in the two emit paths.
Changes:
- Handle
NaN/±Infinityexplicitly intransformEnumDeclarationbefore thevalue >= 0sign check. - Apply the same handling in
EmitResolver.CreateLiteralConstValue. - Add a compiler test case with baselines covering
NaN,-NaN,Infinity, and-Infinity.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/transformers/declarations/transform.go | Guard NaN/Infinity before sign check in enum declaration transform |
| internal/checker/emitresolver.go | Guard NaN/Infinity in CreateLiteralConstValue |
| testdata/tests/cases/compiler/declarationEmitEnumNaN.ts | New test case |
| testdata/baselines/reference/compiler/declarationEmitEnumNaN.js | New baseline showing correct NaN/Infinity emission |
| testdata/baselines/reference/compiler/declarationEmitEnumNaN.types | New types baseline |
| testdata/baselines/reference/compiler/declarationEmitEnumNaN.symbols | New symbols baseline |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NaNcomparisons always returnfalsein IEEE 754, sovalue >= 0falls into the negative branch, emitting-NaNinstead ofNaN. Same issue withInfinity.IsNaN()andIsInf()checks before the sign check intransformEnumDeclarationandCreateLiteralConstValue, matching the existing pattern already used inconstantExpressionand the const enum inlinerNaN,-NaN,Infinity,-Infinityenum member values