Skip to content

Inconsistent behaviour with Object.values and enums with number values #30487

Closed
@BennoDev

Description

@BennoDev

TypeScript Version:

Version used: 3.3.3333

I have reproduced this using typescript/@next which at the time of writing is 3.4.0-dev.20190319

Search Terms:

object.values
object.values enum
enum values

Code

enum DirectionNumber {
    Down,
    Neutral,
    Up
}

console.log(Object.values(DirectionNumber);

// output ["Down", "Neutral", "Up", 0, 1, 2]

Expected behavior:

Expecting consistent output, logging the enum values correctly.

This expected behaviour is also shown in the provided playground where I compare it with the erroneous behaviour.

Actual behavior:

When Object.values gets passed an enum with numbers as values, the resulting array contains both these numbers AND the keys. This seems unintuitive and unintended behaviour.

Playground Link:

Playground link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions