Relying on hacks for production code is usually a red flag/code smell that indicates there was probably a better alternative. `string & {}` definitely feels like one of those hacks.
As an alternative, wouldn't it be better to map the list of supported values in a structure like an enum?
Example:
```
enum HelloWorldValue {
Hello: 'hello',
World: 'world'
}
const value = HelloWorldValue.Hello
```
And now you can call that enum when declaring a variable value and get its contents in autocomplete. As a nice bonus, because these are references and not strings, you can ask your IDE for all places instantiating those values, which can be a life saver.
This is what happens when you are building rules are around an existing system versus the other way around.
In another language you have a value of type String which can take any value, might crash, might not, when you give it a color value.
And then you have a set of items like you described, which are convertible to String (or are a string, like your enum).
In fact, the receiver property probably shouldn't even be String, but typeof Color, which can be constructed through a method/constructor that ensures the String input is a valid format.
This is a neat trick especially if your brain got rewired to think about union and disjoint types. I recently had to do something like this to force separatuon of required arams that go into a media control wrapper:
type ColorHexValue = `#${string}`
type ColorRGBValue = `rgb(${number},${number},${number})`
At least one developer will ask to find an alternative -- use actual CSS names or use a function to create these strings, treat them as strings and forget all those funky typing, becausenobody knows what color a combination of HSV values looks like and nobody would be able to maintain that code. Just because it is possible doesn't mean people should use it.
I like CSS, but the article (while interesting and informative) give me PTSD thinking about a previous project with "typed" CSS. Maybe I'm an old fart, but I never saw the appeal in having such a thorough level of typing.
The downside is longer time to analyse your Typescript code, having to reference some ungodly type if you need to pull your value from a config file, while the upside is just... something that most IDE already did anyway?
In most case, I'm pretty sure it's less of an hassle to say it's a damn string and check that your code give some correct value there, instead of trying to rube-goldberg Typscript into auto-generating the correct code.
I don't understand what you meant by "nobody knows what color a combination of HSV values" (but you quote HEX strings)? What makes the quoted code hard to maintain?
That’s a cool insight about TypeScript’s type handling! Since it’s just about one specific case, the title of the blog post should ideally be a bit more specific, in my opinion.
I'm by no means, a typescript expert - but this seems like over-engineering to me, types are nice, but you don't need perfect typing, in fact it can drastically slow you down.
There needs to be a good medium, I'll add as much to my code so my ide can decorate things whether it's php @var docs, or typescript, but typing every string would drive me mad, now -- if it comes from the DB, that's a whole different story.
Wow I was struggling with this exact problem earlier today. I scraped a list of all css color names into a union so I could get them in my autocomplete, but also wanted to allow any valid css color without having to make template literals for every possible valid color string.
awesome! author here. I was at the same place and was looking with others and we found this in a corner of the internet. now it's on two corners of the internet, hopefully it will be easier for others to find.
you have made my day, knowing it helped at least one person!
The aim here is less about the types (like you say, any string will be valid) and more about autocomplete. When you start a string literal, you can technically type anything in there, but certain answers are more "correct" than others - i.e. these theme colour names. This technique allows you to tell Typescript-as-LSP which names it should suggest, while also letting Typescript-as-typechecker know that other options are also allowed.
This is both a really useful trick but also, I think, way too pally with the exact implementation. It feels like Microsoft could “fix” this at any moment. I mean, they probably won’t…
As an alternative, wouldn't it be better to map the list of supported values in a structure like an enum?
Example:
```
enum HelloWorldValue {
}const value = HelloWorldValue.Hello
```
And now you can call that enum when declaring a variable value and get its contents in autocomplete. As a nice bonus, because these are references and not strings, you can ask your IDE for all places instantiating those values, which can be a life saver.