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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Inference for Merge and MergeDeep #958

Open
SaintPepsi opened this issue Sep 25, 2024 · 2 comments
Open

Key Inference for Merge and MergeDeep #958

SaintPepsi opened this issue Sep 25, 2024 · 2 comments

Comments

@SaintPepsi
Copy link

SaintPepsi commented Sep 25, 2024

I'm not sure if this is even possible in, but would doesn't hurt to ask I reckon.
Would it be possible to have the second parameter for Merge and MergeDeep have the keys of the type you're passing into it be provided as possible overrides/restrictions?

type Foo = {
    foo: string;
    bar: {
        baz:  number
    }
};

type MergedType = Merge<Foo, { baz: string } >; // <-- `baz` would cause an error because the key does not exist on `Foo`
type MergedType = MergeDeep<Foo, { bar: { foo: number } } >; // <-- `bar.foo` would cause an error because the key `foo` does not exist on `Foo['bar']`

We've built an automated type harvesting system that collects a lot of type data to be used by the frontend and sometimes we have to correct those types deeply

At the moment Merge works as expected, but if any of the types for the response data changes we don't know that because Merge does not care whether or not the second argument matches the structure of the first.

Also apologies if this already exists, there's so many types in this library I can't wrap my head around all of them.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

It may be possible, but should be separate types from Merge. Maybe something called MergeStrict or MergeExact.

@SaintPepsi
Copy link
Author

What would be a starting point? I've only really done string inference.

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

No branches or pull requests

3 participants