Skip to content

Fix -NaN emission for enum members in declaration output#3931

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tsgo-enum-nan-issue
Open

Fix -NaN emission for enum members in declaration output#3931
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tsgo-enum-nan-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

NaN comparisons always return false in IEEE 754, so value >= 0 falls into the negative branch, emitting -NaN instead of NaN. Same issue with Infinity.

export declare enum E {
    A = -NaN  // tsgo emits -NaN, should be NaN
}
  • Added IsNaN() and IsInf() checks before the sign check in transformEnumDeclaration and CreateLiteralConstValue, matching the existing pattern already used in constantExpression and the const enum inliner
  • Added test case covering NaN, -NaN, Infinity, -Infinity enum member values

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
Copilot AI requested a review from jakebailey May 16, 2026 20:26
@jakebailey jakebailey marked this pull request as ready for review May 16, 2026 20:35
Copilot AI review requested due to automatic review settings May 16, 2026 20:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/±Infinity explicitly in transformEnumDeclaration before the value >= 0 sign 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsgo emits -NaN for enum members with NaN value in declaration output

3 participants