Hacker News new | ask | show | jobs
by hassleblad23 480 days ago
Its a shame because I like the enum way of declaration a lot more.

`const Foo = { Bar: 'bar' } as const` - this just feels a bit weird.

2 comments

That's a taste thing. Personally, I like my TypeScript as a superset of JS with types, so I dislike all the custom value-space syntax.

`const Foo = { Bar: 'bar' }` is how I would write an enum-like object in JS, so that's how I want to write it in TypeScript, just with added types.

Yes, why do we have "const" at the beginning AND end?
The `as const` at the end will ensure the type of `Foo` is not widened to a `string`.
I don't know JavaScript very well, so I'll take your word for it. Seems like a language flaw to me, though. How many times should you have to say something?