How do I pick two different paths of an object that have different subobject names, for example:
interface Test {
foo: {
foofoo: string;
};
bar: number;
unneeded: null;
}
type pickedTest = O.P.Pick<Test, ['foo' | 'bar', 'foofoo']>;
I want pickedTest to be
type pickedTest = {
foo: {
foofoo: string;
};
bar: number;
}
but the resulting type is
type pickedTest = {
foo: {
foofoo: string;
};
}
which kinda makes sense.
What is the expected syntax here and if that's even possible?
Hi @mifozski,
By default, it does not include something that does not have a valid path.
However, we could write another version called PickAll that would solve your use case:
Imagine the following scenario:
interface Test {
foo: {
foofoo: string;
barbar: string
};
bar: {
foofoo: {
barbar: string
yogyog: string
}
};
unneeded: null;
}
type pickedTest = PickAll<Test, ['foo' | 'bar', 'foofoo', 'barbar']>;
and the ouput type would be:
type pickedTest = {
foo: {
foofoo: string;
};
bar: {
foofoo: {
barbar: string;
};
};
}
Is it what you need? If so, do you find the name convenient (do you have a better idea)?
PickLazy, PickTry, PickAll. The name must start with Pick, as it's my convention.
So the type just picks whatever it can, trying as it dives.
Maybe I could make it the default behavior... Have to think about it.
Strangely enough I was trying to solve the exact some problem in my library. 馃榾
Must be really useful then!
@ctrlplusb what do you think? Should I make this the default behavior?
I think it makes sense to make it a default behavior as I can't think of a case where we'd want to pick several paths at first and then narrow it down to something that will be valid only for one path.
Yep, I agree, I thought this would have been the default behaviour too. :)
Well, thanks, closing the debate. I agree that it is inconsistent. Working on it.
@mifozski @ctrlplusb, it's out ! :timer_clock:
Thanks for reporting this bug
Woot! Gonna experiment with this soon. 馃帀
I must say, this was quite a challenging type to write. I am still wondering if it could be simplified :thinking:
Most helpful comment
Must be really useful then!
@ctrlplusb what do you think? Should I make this the default behavior?