Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom merge types #318

Open
moteus opened this issue Sep 17, 2022 · 0 comments
Open

Custom merge types #318

moteus opened this issue Sep 17, 2022 · 0 comments

Comments

@moteus
Copy link

moteus commented Sep 17, 2022

馃崺 Feature Request

I am working on a typesafe merge library, and I want to support special merge modes for arrays (e.g. concat)
So in runtime code will looks like merge({a: [{b: 1}, {c: 2}]}, {a: merge.ArrayConcat([{d: 3}])})
And result will be {a: [{b: 1}, {c: 2}, {d: 3}]}
One way to achieve such type is to use the L.Assign type but with L.Merge with custom basic merger like

// Here user logic
type MergeConcat <O1, O2 extends MergeArrayConcat<unknown>> =  
  O2 extends MergeArrayConcat<infer E> ? 
  O1 extends L.List ? L.Append<O1, E> : E
  : never

// Overwrite default behavior of L.Merger for the specific type.
type CustomMerge<O1, O2> = O2 extends MergeArrayConcat<unknown> ? MergeConcat<O1, O2> : DefaultMerge<O1, O2>

Here DefaultMerge is what the library uses right now to merge objects.
I am quite new to TypeScript and maybe you know a better solution for this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant