Search posts, tags, users, and pages
Great Article!
By the way, I had this question in my mind:
Is using enum in switch ... case a good idea?
I avoid the enum type at all costs. They're poorly implemented in TypeScript, they have unexpected behaviour at runtime that can catch developers off guard, and they're overly complex. It's very easy to misuse them, and aligning a team on one way of using them is difficult.
Instead, it's easier to either use union types, i.e.
type MyUnion = 'FOO' | 'BAR' | 'FOOBAR';
or as const objects
const SOME_CONSTANTS = {
FOO: 'SOME_VALUE',
BAR: 'OTHER_VALUE',
} as const
Thanks a lot for explaining this to me!
This is also bugging my mind since using enum means you don't need to remember the exact literal string like "PENDING_VERIFICATION". You just define
enum AccountStatus {
PendingVerification = 'PENDING_VERIFICATION';
Active = 'ACTIVE',
Locked = 'LOCKED'
}
and use VSCode autocomplete feature.
E.g. AccountStatus.Pend- => AccountStatus.PendingVerification
VH VSCode will still autocomplete a union of string literals for you, e.g.
type Foobar = 'FOO' | 'BAR';
const doFoobar = (prop: Foobar) => prop;
When you use doFoobar VSCode will suggest/autocomplete once you have typed doFoobar(
Anthony Manning-Franklin Yep it sure supported autocomplete, my bad @@ and I also found out about how enum wasn't that good in TypeScript so I have fully switched to Union Type now.
Ah yes thank you for these awesome blog plots. I have implemented some in my project. I also ask questions in some of your article's comment section. Can you please check them :")