TypeScript Magic
artsy.github.io
artsy.github.io
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.
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.
Because which color is "XXXXXX"?
exactly right!
type ControlParams = { type: 'audio' } & ControlParamsAudio | { type: 'video } & ControlParamsVideo
const ControlsWRapper : React.FC<ControlParams> = <>....</>
Boom, you have proper autocomplete and typechecks for audio controls or for video controls depending on your type. async fetchApi<TResult = void>(request: Request): Promise<TResult> {
type SuccessfulApiResponse = { Succeeded: true; Result: TResult }
type FailedApiResponse = { Succeeded: false; Error: string | null | undefined }
type ApiResponse = SuccessfulApiResponse | FailedApiResponse;
const response = await fetch(request);
ensureStatusOK(response);
const result = await response.json() as ApiResponse
if (!result.Succeeded) throw new Error(result.Error || 'Unknown API error');
return result.Result
}Not that I mind this at all. Autocomplete is a first class concern for all my typescript work.
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.
Typescript is powerful, but KISS is good too.
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.
Stealing this!! Thanks for sharing!
you have made my day, knowing it helped at least one person!
But it's still unintuitive enough that it should only be used in production with a reference to this article.
Edit: Ah I see, this only affects tooltips/autocomplete, doesn't actually change the behaviour of the type.
If you want to do that you need to do something like:
type UserId = number & { __type: 'UserId' };