Skip to content

Enum const members set to -0 get compiled into 0Β #61993

Open
@CraigMacomber

Description

@CraigMacomber

πŸ”Ž Search Terms

enum constant minus-zero -0

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried ( 3.3.3 - 5.9.0-dev.20250702 )

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250702#code/KYOwrgtgBAGlDeAoKKoBkCWAXYAnAhgDZQC8UAtAAyIC+iiAxgPYgDOThwAdIUwOYAKAPIAjAFbAGWLhlYCYXTDgKEANBUoBKTQG5EQA

πŸ’» Code

enum X {
    Literal = -0
}

console.log(Object.is(X.Literal, -0));

πŸ™ Actual behavior

Generates

"use strict";
var X;
(function (X) {
    X[X["Literal"] = 0] = "Literal";
})(X || (X = {}));
console.log(Object.is(X.Literal, -0));

πŸ™‚ Expected behavior

Either Generates an enum member with a value of -0, or an error.

"use strict";
var X;
(function (X) {
    X[X["Literal"] = -0] = "Literal";
})(X || (X = {}));
console.log(Object.is(X.Literal, -0));

Additional information about the issue

The reverse mappings in this case might be problematic since both 0 and -1 stringify to 0. That said, TypeScript is already ok with multiple entries that have the same value overwriting the reverse mapping (and even the enum entries themselves in the case of infinites and NaN) so I think it would be fine. That issue though is why it might be desirable to error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions