Open ssnielsen opened 1 year ago
could be fixed easily by changing the signature of the update function with
export declare function update<T, K extends keyof T, R>(dict: T, key: K, fn: (value: T[K]) => R): {[Kx in Exclude<keyof T,K>]:T[Kx]} & {[Kx in K]:R} & {};
here's the result in my local environment
the output type could be optimized, by removing &
with export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
for better readability.
btw, unlike other fp languages, typescript can infer keyof T type in compile-time, so the value of T[K] should be typeof T[K] instead of Option<T[K]>, I guess.
revisited this issue, seems D.update has been fixed in 4.0.0, but updateUnsafe isn't.
I came across a use-case of
D.update
andD.updateUnsafe
that gave me a bit of trouble wrt. its typing.Example (granted, a bit contrived):
Intuitively, I'd expect the type of
myChangedObject.name
to bestring[]
, but in practice, it becomesstring & string[]
(i.e. the original type ofname
is combined with the return type of the updater function).Looking at the typings of
D.update
andD.updateUnsafe
, the result of my example definitely makes sense, but I'm wondering a bit why the typings includes the original type of the updated property to begin with 🤔Happy to hear your takes on this!