4 ms·
Typescript is great for this. function operation(user: User, state: “active” | “inactive”): void Boom. Done. Enum defined. You want people to use enums? R
by teaearlgraycold 2y ago
Typescript is great for this.
function operation(user: User, state: “active” | “inactive”): void
Boom. Done. Enum defined. You want people to use enums? Remove all context switching from the definition process.
- leobg 2y agoSame in Python, no? def foo(bar: Literal["active", "inactive"])
- hardwaresofton 2y agoWhat's the best Python type checker these days? Seems like there are at least 4: Mypy, Pytype, Pyright, and Pyre
- Terretta 2y agobasedpyright: https://github.com/DetachHead/basedpyright https://github.com/DetachHead/basedpyright (assuming you can't/don't use MSFT's pylance)
- hardwaresofton 2y agoAh I love Typescript (IMO JS is the best "scripting language" out there, and has been for a very long time, but that's a different discussion). That said, I hate that typescript has many ways of doing it. That's the biggest problem. There's: enum SomeEnum { First, Second, } const enum SomeConstEnum { A = 1, B = A * 2, } type SomeEnumType = "first" | "second"; const SomeObjectThatIsAnEnum = { "first": 1 "second": 2 } as const I always lean towards the `enum` keyword, because I am of the opinion that if you're optimizing for less generated JS (and enums are actually worth optimizing for in this way) you're probably using the wrong language to begin with (unless you're in the browser and have no choice, etc).
- ikekkdcjkfke 2y agoTypescript type system is probably gold standard, you can enforce so much statically
- hardwaresofton 2y agoAs far as gold standards go for typesystems... ML languages probably take it, with Haskell being the closest to widespread real world use.
- teaearlgraycold 2y agoIt's the gold standard for languages you'll be able to use at work.
- CharlieDigital 2y agoTypeScript enums, from personal experience, should be avoided in cases where the value is stored. The reason is that the compilation from `SomeEnum` to JS involves conversion to an object indexer[0]. Should you, in the future, need to (or accidentally) remove or change an enum option, this will fail with an index out of range exception at runtime that is not obvious to track down. Put it another way: it leaves behind an artifact in the output JS that can be a source of exceptions. [0] https://www.typescriptlang.org/play/?ssl=5&ssc=2&pln=1&pc=1#code/KYOwrgtgBAwg9gGzgJwM5QN4Cgq6gJWABMAaHPAcWWFDLygCEExgsBfIA https://www.typescriptlang.org/play/?ssl=5&ssc=2&pln=1&pc=1#...
- hardwaresofton 2y agoSorry, can you explain this more? I somehow still don't understand. How is this different from a const object style enum? Or are you more referring to the sum type enum as the better choice here to avoid this. Generally, if you remove an enum variant, the compiler should warn you if you've used it anywhere else (though of course it can't figure out a dynamic usage)... But this seems like a problem mostly for dynamic code, where you'd probably write some sort of Enum.parse() or Enum.from() static method anyway? If I'm understanding your comment correctly, the real problem is that when you store the value, remove one, and try to read it back out into the enum it will fail - but in the code I've written so far trying to construct the the enum object almost always consisted of using a function to validate anyway, these days I use zod but before I'd write stuff like: function isEnum(obj: unknown): obj is Enum { return obj && typeof obj === 'string' && Object.values(Enum).includes(obj) }
- ivan_gammel 2y agoThat’s the worst way to define enums. Now you have to make sure your codebase is using the same string literals everywhere and carry the potentially long type definition to every parameter, variable or function return type. And no, compile-time checks won’t cover 100% of cases.
- teaearlgraycold 2y agoObviously if this gets reused you need to put it in a central definition.
- SkyPuncher 2y agoThe only place I believe this pattern is acceptable is option props in React components. Otherwise, it becomes an absolute mess of “almost” defined correctly.
- ivan_gammel 2y agoHow wouldn’t it be reused? Besides, what will happen if you need to change a constant in the enum from „a“ to „b“? Compiler won’t help you to spot all the places like ˋˋˋif (value == „a“)ˋˋˋ, it will quietly become unreachable in runtime.
- teaearlgraycold 2y agoYou underestimate TypeScript. Your example will give a compiler error: This comparison appears to be unintentional because the types '"a"' and '"b"' have no overlap.(2367) https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZiEAuGAiAhqmBeNmDcAUIQJZwwAUCIO2uqARqgJQwDehMXMokIANgFMAdPxABzChgCeIFkQC+hIA https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZiEAu... It's features like these that make Python/Ruby/etc. type-checkers insufficient substitutes for TypeScript. I think my example wasn't the best. Of course "active" | "inactive" would get reused. But if it's just a little function flag it could easily be a one-off.