Typescripts inferred types, can do some crazy things, and can be very powerful. This to me is really fun, but has definitely caused me when I first started using it to get lost in having fun doing crazy things, to ensure my strings were exactly some set of values depending on what some other value was, when a simple unit test and a simple "string" type, would've been a lot quicker and less confusing.
If the strings are static you can define a type that's a union of string literals, create a tuple containing them all, and then a type guard function that simply checks the argument against the array. If the strings are dynamic then you're better off looking at a library like newtype-ts, but it's not very ergonomic compared to the likes of Haskell/PureScript so use sparingly.
Just my personal preference but using these string union types means that you don’t have to import the enum. Plus it’s less verbose and translates back to JS better than an enum does.
Yeah the stuff I was doing was more to see how crazy of things we could do with the type system. Like I said just fun. But probably a waste of time other than the learning. Just a warning that is tempting to do crazy (but stupid) things.
One kind of weird thing we did, was we had these JSON Schema definitions that were const, so all the strings like "type" were of a literal string type. Then using recursive type definitions, and the `extends` key word we were able to extract the actual shape of the data into a type.
Not exactly that useful, compared to typing it out, but it was still kind of cool.
Typescripts inferred types, can do some crazy things, and can be very powerful. This to me is really fun, but has definitely caused me when I first started using it to get lost in having fun doing crazy things, to ensure my strings were exactly some set of values depending on what some other value was, when a simple unit test and a simple "string" type, would've been a lot quicker and less confusing.