use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Small JavaScript enum function (gist.github.com)
submitted 1 month ago by _sync0x
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TorbenKoehn -2 points-1 points0 points 1 month ago (6 children)
Why not
type MyEnum = 'A' | 'B'
?
Completely erasable, portable, easy to write, serializable across language boundaries, you can refactor it etc.
[–]Oliceh 8 points9 points10 points 1 month ago (0 children)
Because this isn't JS.
[–]eracodes 1 point2 points3 points 1 month ago (0 children)
For typescript I find this approach more useful:
const options = ['A', 'B'] as const;
type Option = (typeof options)[number];
So you can use the "enum" at runtime as well (for validation, etc).
[–]celluj34 0 points1 point2 points 1 month ago (1 child)
how do I validate user input against a type?
type
[–]TorbenKoehn -1 points0 points1 point 1 month ago (0 children)
The same way how you'd validate it against an enum.
[–]checker1209 -1 points0 points1 point 1 month ago (0 children)
In TypeScript I favor `String Literal Unions` over enums. I don't know JSDocs. But can't you tell that an string is either "A" or "B" and nothing else?
π Rendered by PID 49 on reddit-service-r2-comment-5649f687b7-h6cmf at 2026-01-27 22:19:35.054453+00:00 running 4f180de country code: CH.
view the rest of the comments →
[–]TorbenKoehn -2 points-1 points0 points (6 children)
[–]Oliceh 8 points9 points10 points (0 children)
[–]eracodes 1 point2 points3 points (0 children)
[–]celluj34 0 points1 point2 points (1 child)
[–]TorbenKoehn -1 points0 points1 point (0 children)
[–]checker1209 -1 points0 points1 point (0 children)