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

Merge is not that useful with indexed types #382

Closed
augustobmoura opened this issue Mar 23, 2022 · 3 comments · Fixed by #455
Closed

Merge is not that useful with indexed types #382

augustobmoura opened this issue Mar 23, 2022 · 3 comments · Fixed by #455
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@augustobmoura
Copy link

augustobmoura commented Mar 23, 2022

When using Merge with an indexed type on either left or right hand all the well-known properties are lost. This is because keyof of an indexed type always returns 'string | number', because every key is possible in that object.

Example:

type Example = {
  foo: string,
  bar: symbol,
  [key: string]: unknown,
}

type Foo = {
  bar: Date,
}

/*
  We would expect something like:
  {
    foo: string,
    bar: Date,
    [key: string]: unknown,
  }
  
  but it actually returns:
  {
    [key: string]: unknown,
    [key: number]: unknown,
  }
*/
type result = Merge<Example, Foo>

This is a well-known caveat with Typescript that was discussed in a few issues (microsoft/TypeScript#45367, microsoft/TypeScript#31153), the behavior is actually intended.

And Omit indeed loses the well-known keys in the same way:

/*
  Returns:
  {
    [key: string]: unknown,
    [key: number]: unknown,
  }
*/
type omit = Omit<Example, 'foo'>

/*
  Our current implementation of Except on the other hand works fine with indexed types
  This returns:
  {
    bar: Date,
    [key: string]: unknown,
  }
*/
type except = Except<Example, 'foo'>

What I propose is to either change Merge to support indexed types and return what I'm expecting on the example (similar to except), which could be a breaking change for people expecting to lose typing on these cases (but I doubt is a useful behavior), or we can create a new MergeWellKnownKeys that treats indexed types. What do you guys think?

I can work on a PR as soon as we decide on an implementation. It should be easy because we already have an internal type doing the same thing on our company's project

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

What I propose is to either change Merge to support indexed types and return what I'm expecting on the example (similar to except)

I agree. That's the expected behavior. I would consider this a bug fix.

@sindresorhus sindresorhus added bug Something isn't working help wanted Extra attention is needed labels Mar 30, 2022
@skarab42 skarab42 mentioned this issue Aug 29, 2022
Closed
5 tasks
@skarab42
Copy link
Collaborator

skarab42 commented Aug 31, 2022

@sindresorhus If you want I can fix this before v3 (#387) because if we change the return to integrate the indexes it becomes a bracking change.

@sindresorhus
Copy link
Owner

@skarab42 That would be great 👍

@augustobmoura augustobmoura changed the title Merge doesn't is not that useful with indexed types Merge is not that useful with indexed types Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants