You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
setPath: throws on missing optional path segments #554
Hello, looking at the setPath logic, it seems to check if the path value on the object is not null/undefined before attempting to insert the new value. How can we set a new value to the object that is not yet defined?
Hey, yeah i figured if proper typing is a tenet of this library, it's gonna be hard to infer/produce a dynamic, nested property...
It would be awesome, though. Not sure if one can create a dynamic type from a path, and make a union of the input param generic type with that dynamic type to type the output.
I stumbled upon a similar problem recently - I think it technically might even be possible to write a function like that type-safely(ish), but there might be too many decisions on how to handle edge-cases to have this on a library level.
For example, what if the path contains an array index that is non-zero, but its parent property does not exist in target? Do you put the value at index 0 (thus not really upholding contract), or risk creating a sparse array?
I'm not sure there are canonical answers to this and other edge-case questions, so it feels like something that should remain in user-land.
I ended up ignoring array properties (didn't need them) and recursively creating a new object using objOf + some type magic to create a type from path in the end.
Activity
cjquines commentedon Mar 2, 2024
the trouble with this is with undefined deeply nested paths:
there's no way to do this type-safely if
obj.testisn't definedbombillazo commentedon Mar 2, 2024
Hey, yeah i figured if proper typing is a tenet of this library, it's gonna be hard to infer/produce a dynamic, nested property...
It would be awesome, though. Not sure if one can create a dynamic type from a path, and make a union of the input param generic type with that dynamic type to type the output.
Brodzko commentedon Apr 1, 2024
I stumbled upon a similar problem recently - I think it technically might even be possible to write a function like that type-safely(ish), but there might be too many decisions on how to handle edge-cases to have this on a library level.
For example, what if the path contains an array index that is non-zero, but its parent property does not exist in target? Do you put the value at index 0 (thus not really upholding contract), or risk creating a sparse array?
I'm not sure there are canonical answers to this and other edge-case questions, so it feels like something that should remain in user-land.
I ended up ignoring array properties (didn't need them) and recursively creating a new object using
objOf+ some type magic to create a type from path in the end.eranhirsch commentedon Jul 30, 2025
This is actually a bug! setPath can lead to an error being thrown, and the typing doesn't do enough to protect against it:
[-]`setPath` only works for existing properties[/-][+]setPath: throws on missing optional path segments[/+][-]setPath: throws on missing optional path segments[/-][+]`setPath`: throws on missing optional path segments[/+]