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

feat(assign): refine types #141

Closed
wants to merge 2 commits into from

Conversation

crishoj
Copy link
Contributor

@crishoj crishoj commented Jul 28, 2024

Summary

The type declaration with assign (mistakenly?) requires that both arguments have the same shape.

Taking the example from the docs:

image

Two warnings:

image image

After:

image

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

@crishoj crishoj requested a review from aleclarson as a code owner July 28, 2024 00:01
export function assign<
X extends Record<string | symbol | number, any>,
Y extends Record<string | symbol | number, any>,
>(initial: X, override: Y): X & Y {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, X & Y is not representative.

type A = {a: number}
type B = {a: string}

type C = A & B

declare const c: C
c.a // number & string

Link to above example in playground.

We actually want c.a to result in string (as string will always override number). If B["a"] was possibly undefined like {a?: string}, then c.a should result in number | string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, A & B does not result in the desired type.

Perhaps something like the Merge TS helper from https://github.com/millsp/ts-toolbelt/blob/master/sources/Object/Merge.ts could work?

Copy link
Member

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment ✌️

@aleclarson
Copy link
Member

Thanks for the contributions, @crishoj!

I agree the current assign types are not good :)

@aleclarson
Copy link
Member

Closed in favor of #142

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

Successfully merging this pull request may close these issues.

2 participants