You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enumerate String Literal Union Values at Runtime #13542
// Would like to do this:typeGreeting='hi'|'hola'|'hallo'|'salut';constallGreetings: Greeting[]=keyofGreeting;// Error!// Currently, I am forced to do this:typeGreeting='hi'|'hola'|'hallo'|'salut';constallGreetings: Greeting[]=['hi','hola','hallo','salut'];
Expected behavior:
I have a need to get all values from a String Literal Union Type at runtime as an array. It would be great if the keyof operator (or some other operator) provided this functionality.
Actual behavior:
Currently, there is no way to do this besides just manually copying all of the values into an array.
I believe that would suit my needs. However, assuming that the language had both string enums and string literal unions, I think I would be torn between which to use. Would the rule be to use a string enum when you want to enumerate the values and otherwise use a string literal union?
They both seem to accomplish the same thing in different ways.
I think the difference would simply be whether or not you wanted the strings to be available at runtime. The type system is always fully erased so there's not going to be a way to emit different code depending on what a type system object is (see https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals)
Just out of curiosity, how is it that enums don't break the "fully-erasable type system" rule? I mean, enums don't exist in JavaScript. So I would consider them to be part of the type system, but they are not erased at runtime. They are compiled into a two-way associative array. So how would it be any different to emit an array of string literal values at runtime? I just want to increase my understanding. Thanks!
The type system is erased (consider how generics are erased in Java even though generic classes still create code) but declarations which are specified to create values still create values.
The distinction here is that you can syntactically analyze (not typecheck) a file and produce the correct JavaScript; this would not be true for an unerased type system. Basically, the type of any given expression (as determined by the checker) does not change how it behaves at runtime.
Ryan Cavanaugh (@RyanCavanaugh) thanks for the quick reply and such a good reply at that! It's going to take some time for me to digest everything that you said.
But in the meantime, it sounds like string-based enums are the way to go. So I'm going to close this bug.
Activity
RyanCavanaugh commentedon Jan 17, 2017
Is #3192 what you actually want?
battmanz commentedon Jan 17, 2017
I believe that would suit my needs. However, assuming that the language had both string enums and string literal unions, I think I would be torn between which to use. Would the rule be to use a string enum when you want to enumerate the values and otherwise use a string literal union?
They both seem to accomplish the same thing in different ways.
RyanCavanaugh commentedon Jan 17, 2017
I think the difference would simply be whether or not you wanted the strings to be available at runtime. The type system is always fully erased so there's not going to be a way to emit different code depending on what a type system object is (see https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals)
battmanz commentedon Jan 25, 2017
Just out of curiosity, how is it that enums don't break the "fully-erasable type system" rule? I mean, enums don't exist in JavaScript. So I would consider them to be part of the type system, but they are not erased at runtime. They are compiled into a two-way associative array. So how would it be any different to emit an array of string literal values at runtime? I just want to increase my understanding. Thanks!
RyanCavanaugh commentedon Jan 25, 2017
The type system is erased (consider how generics are erased in Java even though generic classes still create code) but declarations which are specified to create values still create values.
The distinction here is that you can syntactically analyze (not typecheck) a file and produce the correct JavaScript; this would not be true for an unerased type system. Basically, the type of any given expression (as determined by the checker) does not change how it behaves at runtime.
battmanz commentedon Jan 25, 2017
Ryan Cavanaugh (@RyanCavanaugh) thanks for the quick reply and such a good reply at that! It's going to take some time for me to digest everything that you said.
But in the meantime, it sounds like string-based enums are the way to go. So I'm going to close this bug.
pelotom commentedon Feb 27, 2017
Hi, check out my library runtypes for a solution to this problem. Example usage: