Skip to content

string enum member used as "computed enum member" types as number. #40862

Closed
@tadhgmister

Description

@tadhgmister

TypeScript Version: all the ones in playground, latest is v4.1.0-dev20201001

Search Terms:

enum, incorrect type, computed enum, number, string

Code

enum Constants {
    hello = "Hello"
}
enum Potato {
    // THIS IS CONSIDERED A NUMERIC ENUM!!!!!
    h = Constants.hello
}
// Error: Type 'Potato' is not assignable to type 'string'.(2322)
const value: string = Potato.h;

// intellisense shows Potato.h = "Hello", but tsc compiles as though it's a number
console.log(Potato.h.toExponential());

Expected behavior:

Using a string enum member as the computed key in another enum should either be considered a string enum or be not allowed.

Actual behavior:

Potato.h is typed as number despite being assigned to a string. Potato.h.toExponential() passes type checking and throws runtime error.

Playground Link

Related Issues:

https://stackoverflow.com/questions/64145849/how-can-i-pass-an-enum-with-keys-of-another-string-enum-into-a-function-that-a

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions