-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Union from values in an object #17908
Comments
#17910 - Pull Request updated. |
type AnyMeasurementUnit<T> = {
[K in keyof T]:T[K] extends readonly any[]?T[K][number]:never
}[keyof T] @koodiohto Your description is too specific. It's better to have a broader description |
Thank you for your feedback and one possible solution! I'd appreciate if you still have time to point out which part of the description in specific you think should be made broader. Or do you mean that this example shouldn't be coupled to the measurement units, but be more generic? Thanks! Btw. this is how I ended up solving this (without using generics in the type):
|
Yesterday I edited your test case from Actually, I hope your type Then we can use this type like this : AnyMeasurementUnit<{
onlyLength: ['m','km']
}>
AnyMeasurementUnit<{
onlyWeight: ['g','kg']
}> |
Okay, thank you for your feedback! I was just thinking that my original suggestion was based on an actual "business logic need" which was solved by this AnyMeasurementsUnit-type. So I had the case that I needed to categorize the measurements units, but then I also needed a type for checking that the measurement unit is a valid measurement unit, but doesn't matter to which "measurement unit category" it belongs to. So it's more a type that "validated that a type is a value in another object's arrays". So if I would try to make the question setup in my original idea to be more broader, I would probably propose it to be like this (so it wouldn't be coupled to the measurements units context):
But I definitely agree that I am having hard time of formulating this into an understandable question. And I don't know if this makes for a good general use case with TS that is a good assignment in the first place..? |
But yeah, when still thinking of your good proposal, so maybe it is better to change the assignment description and task to utilize this "generic" type that can be used in a similar fashion as the Partial type. I'll try to modify this next week. Thanks! |
Info
Question
Create a generalizable type UnionOfArrayValuesInObject that can be used to create a union type of all the values from all the array values inside an object that contains object keys and arrays as the objects.
The task is solvable without using utility types.
Template
Test Cases
The text was updated successfully, but these errors were encountered: