I would like to have a type where I know specific properties are going to be defined, but some properties are going to be missing. Something like this:
type UserType = {
email: string
name: {
first: string
last: string
}
address: {
city: string
state: string
zip: string
coordinates: {
lat: number
lng: number
}
}
}
const partialUser: PickPartial<
UserType,
| 'email'
| Record<
'address',
Record<
'coordinates',
| 'lat'
| 'lng'
>
>
>
Basically I am trying to select like this:
{
email: string
name?: {
first: string
last: string
}
address: {
city?: string
state?: string
zip?: string
coordinates: {
lat: number
lng: number
}
}
}
Is anything like this possible? I saw the PartialDeep
code, but that doesn't work the way I want to. I essentially want to say, these specific properties in the tree are defined (not possibly undefined), and the rest are possibly undefined ?
. How can I accomplish that in TypeScript? If not exactly, what is as close as I can get to that, or what are some workarounds?
Another API approach might be:
const partialUser: PickPartial<
UserType,
{
email: string
address: {
coordinates: {
lat: number
lng: number
}
}
}
>
Then everything else gets a ?
possibly undefined key. Is it possible for that to be done somehow?