#Adapt KeyPaths<> type helper to accept partials

1 messages · Page 1 of 1 (latest)

safe helm
#

A previous question helped with getting the navigation/key paths out of an object. But it doesn't work when properties are optional. I've been playing with a related TypeScrip problem 2 1/2 hours and my brain is tired. Can anyone help make this accept DeepPartial<> types and still work?

trim plazaBOT
#
Emtucifor#7044

Preview:ts ... type A = KeyPaths<typeof x>; // ^? type B = KeyPaths<DeepPartial<typeof x>> // ^? ...

safe helm
trim plazaBOT
#
n_n#2622

Preview:ts ... type KeyPaths<T> = { [K in keyof T & string]: T[K] extends | object | undefined ? `${K}.${KeyPaths<T[K] & {}>}` : K }[keyof T & string] ...

sonic stratus
#

@safe helm

#

| undefined + & {}

#

the other common methods (Required<T>[K], NonNullable<T[K]>) seems to trigger a ts(2589)

#

!:2589

safe helm
#

Yes I was seeing the deep and possibly infinite error

sonic stratus
#

the other way to avoid that error is to do TheProblematicType extends infer T ? DoTheThing<T> : never

#

but in this case & {} is probably way simpler

safe helm
#

I've been at work 10 hours, brain is tired. Where does the & {} go?

sonic stratus
safe helm
#

The original code I shared has & {}, the new code you shared seems to add only | undefined? Am I being blind?

UPDATE: Yes, I was being blind, there's an & {} added after the first ?

sonic stratus
trim plazaBOT
#

@safe helm
Because your issue seemed to be resolved, this post was marked as resolved by @sonic stratus.
If your issue is not resolved, you can reopen this post by running !reopen.
If you have a different question, make a new post in #1057653400046674112.

sonic stratus