Skip to content

Object.values(Enum) should return an array of Enum's value's type #33200

Closed
@jimcullenaus

Description

@jimcullenaus

Search Terms: Object.values

Code

enum Role {
	Admin = 'ADMIN',
	Results = 'RESULTS',
	Finish = 'FINISH',
	Config = 'CONFIG'
}

Object.values(Role).forEach(role => {
	console.log(typeof role); // prints 'string' four times
}); 

Object.values(Role).includes('hello'); // errors at build time

Expected behavior: Object.values(EnumType) at build time should be returning an array of the same type as the values of the EnumType.

Actual behavior: Object.values(EnumType).includes('enumValueType') errors out with

error TS2345: Argument of type '"enumValueType"' is not assignable to parameter of type 'EnumType'.

It appears as though, at build time, Object.values(EnumType) is parsed as returning an array of EnumTypes.

Error first encountered on Typescript 3.6.2. Still present as of typescript@next.

Playground Link: Playground link. Playground does not appear to be correctly using ES2017 and Typescript 3.6.2/3.7.0 are not available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions