Typescript: spread operator for enum

Created on 6 May 2019  路  5Comments  路  Source: microsoft/TypeScript

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:

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

Most helpful comment

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'));

All 5 comments

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'));

Thank you, @nickbclifford.

_However_, I'm following a similar approach.

export function enumToArray(enumObject: any): string[] {
  const keys = Object.keys(enumObject).filter(key => typeof enumObject[key as any] === "number");
  return keys;
}
enum TestStatus {
    PENDING,
    ACCEPTED,
    WRONG_ANSWER
}
console.log(...enumToArray(TestStatus));

But imagine, what if the spread operator is natively supported for enums! I think that would provide a better and more concise way to write code.

  • [x] This wouldn't change the runtime behavior of existing JavaScript code

Right now console.log(...MyEnum) will fail because MyEnum has no @@iterator, so this would change that runtime behavior of existing code.

  • [x] This could be implemented without emitting different JS based on the types of the expressions

I don't see how that's possible. Because MyEnum is not an array, array-like, or iterable, console.log(...MyEnum) has to behave differently than console.log(...MyArray).

Assuming the shape of enum objects isn't changing (if it did, that would be a disastrous break), you'd need to have a type-directed transpile where console.log(...MyArray) outputs console.log(...MyArray) and console.log(...MyEnum) outputs console.log(...Object.values(MyEnum).filter(something)

Also,

[x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)

How is this not a runtime feature? The checklist should actually be checked, nit just copied.

Not to mention that piling on one of the exclusive TypeScript code features (namespaces, enums, import=/export=require()) is not a good idea (the rest is just ECMAscript after type annotation removal). Keep TypeScript Javascript (plus types)!

Enums can be had by adding as const to object literals these days.

This is _technically_ feasible by including Symbol.isConcatSpreadable and a Symbol.iterator in our downlevel enum emit, however I doubt we're looking to increase the featurefulness of our nonstandard features, so I don't think this'll go anywhere for awhile.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kimamula picture kimamula  路  147Comments

jonathandturner picture jonathandturner  路  147Comments

fdecampredon picture fdecampredon  路  358Comments

quantuminformation picture quantuminformation  路  273Comments

OliverJAsh picture OliverJAsh  路  242Comments