Skip to content

Negative constant enums return number when toString is called #54992

Closed
@DustinBrett

Description

@DustinBrett

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.1.6#code/MYewdgzgLgBApmArgWxgFTtAok1BvAKBmJgEEYBeGARgBoiSAhSmAWmoIF8CDRIQANnAB0AkAHMAFA2JQAngAc4IAGYwAylABOASzBSM2XMNIBKeiRjylq9Jig4UJ4VBCbd+yaYLfe4CIIiYlIyVorKau56BvaOyMKM5qHWEXZGTowubtrRXj5AA

💻 Code

const enum TestEnum {
    A = 1,
    B = -1
}

// "string",  "string" 
console.log(
    typeof String(TestEnum.A),
    typeof TestEnum.A.toString()
)

// "string",  "number" 
console.log(
    typeof String(TestEnum.B),
    typeof TestEnum.B.toString()
)

🙁 Actual behavior

Constant enums which use negative numbers will return type of number when .toString() is called.

🙂 Expected behavior

A string type should be returned

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions