-
Notifications
You must be signed in to change notification settings - Fork 14
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
Creating subsets from existing unions #16
Comments
You could define const PropertyComparisonSubset = unionize({
SimilarFunctions: ofType<{}>(),
ValuesDeepEqual: ofType<{}>(),
}); and then use it in the definition of const PropertyComparison = unionize({
Same: ofType<{}>(),
...PropertyComparisonSubset._Record,
Unequal: ofType<PropertyComparisonUnequal>(),
}); |
From my testing, this works from the point of view of typings, but it doesn't work at runtime.
Since _Record is undefined, in the above example, the object spread I thinking changing the above return statement should fix this problem:
@pelotom What do you think? Would you accept a PR for this? |
@wmaurer ah, yes, it should return the |
Return _Record that was passed in. Fixes #16
Unfortunately this workaround doesn't help when you want to create multiple (potentially overlapping) subsets, e.g. (pseudocode): type All = Bob | Sarah | Sam;
type LikesCheese = Pick<All, 'Bob' | 'Sarah'>
type IsOld = Pick<All, 'Sarah'>; |
I find myself needing this again and again. As I mentioned in my previous comment, the workaround mentioned doesn't help, because there can be many overlapping subsets. It would be really useful to be able to create an existing unionize union and "pick" from it, which would create a new unionize union (type, |
@OliverJAsh But I could see problems combining existing unionize unions - e.g. what if they had differring |
Hmm, but if we do that, I don't understand how it's possible to match a subset union. Using my example above: type All = Bob | Sarah | Sam;
type LikesCheese = Pick<All, 'Bob' | 'Sarah'>
type IsOld = Pick<All, 'Sarah'>; How would we define a function that checks whether an |
Something like this? // from lodash, ramda or whatever
declare function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K>;
const All = unionize({ Bob: {}, Sarah: {}, Sam: {} });
type All = UnionOf<typeof All>;
const LikesCheese = unionize(pick(All, 'Bob', 'Sarah'));
type LikesCheese = UnionOf<typeof LikesCheese>;
// maybeLikesCheese: (all: All) => LikesCheese | null
const maybeLikesCheese = All.match({ ...LikesCheese, default: () => null }); |
Interesting. I just tried that but ran into this problem: import unionize, { ofType, UnionOf } from 'unionize';
declare function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K>;
const All = unionize({ Bob: ofType<{ name: string }>() });
type All = UnionOf<typeof All>;
const LikesCheese = unionize(pick(All, 'Bob'));
type LikesCheese = UnionOf<typeof LikesCheese>;
// Expected: (value: { name: string }) =>
// Actual: (value?: {} | undefined) =>
LikesCheese.Bob; |
Is it possible to create a subset union type from an existing union type?
E.g. given
type Foo = A | B | C | D
, I want to create another union that is just composed ofA | B
.Perhaps this is a bad idea and I'm looking at this the wrong way. For context, here is why I think I need this.
I have this union:
And this record type:
Now I also want to create a type like
PropertiesComparison
except where the values can only beValuesDeepEqual | SimilarFunctions
:The text was updated successfully, but these errors were encountered: