proposal-enum icon indicating copy to clipboard operation
proposal-enum copied to clipboard

typeof enum is weird

Open ljharb opened this issue 7 years ago • 1 comments

For example, in:

enum Colors { Red, Blue, Green }

typeof Colors.Red // `"enum"`, but it's really a string
typeof (0, Colors.Red) // still "enum"?
String.prototype.toString.call(Colors.red) // throws, because it's not a string? "Red", because it's an object boxing a real string with the right internal slots, but then it's not a primitive which means it shouldn't have its own typeof type?

In other words, if enum is a new primitive type, then I think it can't be a "string" or a "number" or a "symbol", it'd have to be something new - probably something that couldn't be coerced casually to those things.

#2, however, might address this.

ljharb avatar May 15 '18 00:05 ljharb

If we stick with the enum primitive type, then I imagine String.prototype.toString.call(Colors.red) would throw (assuming enum Colors { Red = "Red", ... }), since its neither a String nor an Object with a [[StringData]] internal slot.

rbuckton avatar May 15 '18 01:05 rbuckton