Is there some way to fix these types without casting? #489
Unanswered
codingismy11to7
asked this question in
Q&A
Replies: 1 comment
-
Would this be a good representation of what you are trying to do? import { deepmerge } from "deepmerge-ts";
type Stored = Readonly<{ field1: string; field2?: string }>;
type UpdateStored = Readonly<{ field1?: string; field2?: string }>;
const DefaultStored: Stored = {
field1: "a"
};
const updateStored = (patch: (old?: Stored) => Stored) => patch({
field1: "b"
});
const doUpdate = (update: UpdateStored) =>
updateStored((old) => deepmerge(old ?? DefaultStored, update));
const result = doUpdate({
field2: "c"
}); Because if so, I can't reproduce your issue. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Just picked up this library yesterday, and am trying to figure out why my code doesn't work without casting. I'm doing what I think would be a pretty standard thing for this lib, trying to use it for PATCHes.
i have a stored type and a patch type like (simplified)
where the update fields are all optional and the stored fields are a mixture of required and optional.
i'm using deepmerge in a patch function like
the code and functionality work fine, but unless i cast the
update
parameter ordeepmerge
return withas Stored
, typescript complains that I'm returning anUpdateStored
from thedeepmerge
call and refuses to compile. so right now i'm doingwhich compiles and works but makes me uneasy. just wondering if anybody else has run into anything like this or if i'm doing something obviously wrong
Beta Was this translation helpful? Give feedback.
All reactions