This is what I'm trying to achieve:
interface A {
a: string;
b?: number;
c?: number;
}
type B = PickOptionalKeys<A>;
// B should be { b?: number | undefined; c?: number | undefined }
Couldn't make it work using the existing types … somewhat surprisingly. Am I missing something here?
Wrote this, which is mostly copied from ConditionalKeys
/ConditionalPick
to help me for the moment:
type OptionalKeys<Base> = NonNullable<
{
[Key in keyof Base]: undefined extends Base[Key] // This line is the only difference compared to `ConditionalKeys`
? Key
: never;
}[keyof Base]
>;
type PickOptionalKeys<Base> = Pick<Base, OptionalKeys<Base>>;
Could totally be part of this package, imo. Or should this be tackled in an even broader sense, having the undefined
as a second generic parameter? Can't really see a frequent usecase for that one, though.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too