question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

spread operator for enum

See original GitHub issue

Search Terms

  • Enum
  • Spread operator

Suggestion

I would like to use the spread operator to spread the values (or maybe the keys) of an enum.

Use Cases

I want to use this to check or print available types of enum type.

Examples

enum TestStatus{
    PENDING,
    ACCEPTED,
    WRONG_ANSWER
}
console.log(...TestStatus); // prints: "PENDING"  "ACCEPTED"  "WRONG_ANSEWR"

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:33
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
nickbcliffordcommented, May 6, 2019

This can be done right now with Object.values:

enum TestStatus {
    PENDING,
    ACCEPTED,
    WRONG_ANSWER
}
console.log(...Object.values(TestStatus).filter(k => typeof k === 'string'));
7reactions
walidBouguimacommented, Apr 15, 2021

This would be cool, if added anytime soon. As of now the suggestion of @nickbclifford does the job console.log(...Object.values(Enum)).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to merge two enums in TypeScript - Stack Overflow
Using an enum is simple: just access any member as a property off of the enum itself, and declare types using the name...
Read more >
How to extend enums in TypeScript - LogRocket Blog
The short answer is no, you can't extend enums because TypeScript offers no language feature to extend them. However, there are workarounds you ......
Read more >
Spread Operator - Beginners Guide to TypeScript - Fullstack.io
The spread operator faciliates three common tasks: Copying the elements of one or more arrays into a new array. Copying the properties of...
Read more >
What is the enum in JavaScript? - JS Remote jobs
Defining enum via Object.freeze() · value: the actual value of the property. · enumerable: determines whether the property will appear when ...
Read more >
Enum With and Without Values - Learn TypeScript - Educative.io
A mixed enum value type is acceptable if every member is defined. For example, you can have one item be an integer and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found