Force every value of union type in an array
Let's say that I had the following union type:
If I wanted to have an array that accepted a list of those values I would do something like this
Is there a way to force an error if the array doesn't contain all the values?
15 Replies
the only way I know how to achieve what you want is to do the reverse:
i just googled it and found the answer you're looking for: https://stackoverflow.com/a/60132060/12101554
Stack Overflow
Make sure array has all types from a union
Say I have a union that looks like this
type Colors = 'red' | 'blue' | 'pink'
Is it possible to type check an array against this union and make sure the array contains all of the types?
I.e.:
...
i searched "typescript make array contain all values of a union"
I'm reconsidering my google skills since I googled it for the past 40 minutes
Thank you tho!
Now that I think about it I've been using duckduckgo for a week
It would be cool if you could encapsulate this in a type, I am struggling with the notation
on ddg it's the second result
I should ditch this job
Going farming
Anyhow, thank you once again @Samathingamajig , have a good rest of your day/night!
angular carrot farmer
don't tell me that I got hired to code in vue, which means I now don't deserve to enter the react church anymore
i have to use google since i dont want to switch back and forth every day because my school blocks all search engines excep tgoogle
why would anyone do that
that's one of the dumbest things I've ever heard
sometimes you need to put it in a function, but it boils down to a no-op at runtime (and typescript might even get rid of it all together) since it's just an identity function (returns the input with no extra operations, always)
looks like it doesn't get compiled away
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
How would you guys imagine this feature if it actually existed and didn't need those 3 lines of boilerplate
Hahahah it depends on the day, yesterday I felt godly
Luckily I'm shutting everything down in a bit since I'm almost done