Open WORMSS opened 5 years ago
What problem are you trying to solve? Why do you need to get the optionals?
Specifying what the optional defaults should be.
@WORMSS You're probably looking for OptionalKeys
, as discussed here https://github.com/sindresorhus/type-fest/issues/56#issuecomment-583733537
@sindresorhus Since this seems to be a valid use-case, how would we feel about PRing OptionalKeys
in separately to #56?
Also, as for specifying the defaults, if they're all the same, you can do this.
type AWithDefaults = Omit<A, OptionalKeys<A>> & Record<OptionalKeys<A>, Default>;
Since this seems to be a valid use-case, how would we feel about PRing OptionalKeys in separately to #56?
Sure, but if we add OptionalKeys
, we should probably also add RequiredKeys
.
+1 for OptionalKeys
and RequiredKeys
.
I didn't mind the learning exercise before I found this ticket, but it would have been nice to have it in the library!
My solution was
type OptionalKeys <T> = {
[K in keyof T]: Pick<Partial<T>, K> extends Pick<T, K> ? K : never
}[keyof T]
Happy to PR, but maybe @resynth1943's answer from #56: {} extends Pick<T, K> ? K : never
is nicer.
Anyone know if its possible to get the Optional values of a type?
so B becomes something like
Though for my usecase I will do something like
But I presume that wouldn't make a difference?
Upvote & Fund