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

Add SharedUnionFields type #994

Merged

Conversation

Emiyaaaaa
Copy link
Collaborator

Simple version of SharedUnionFieldsDeep

@Emiyaaaaa
Copy link
Collaborator Author

Emiyaaaaa commented Dec 1, 2024

Hi @sindresorhus, would it be possible to add me to the funding.yml? Thanks a lot!

@sindresorhus
Copy link
Owner

Of course: ca449dc

@sindresorhus sindresorhus merged commit a716c29 into sindresorhus:main Dec 2, 2024
11 checks passed
Copy link
Collaborator

@som-sm som-sm left a comment

Choose a reason for hiding this comment

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

@Emiyaaaaa Was going through this PR, and I have shared some thoughts. Let me know what you think.

// `Union extends` will convert `Union`
// to a [distributive conditionaltype](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
// But this is not what we want, so we need to wrap `Union` with `[]` to prevent it.
: [Union] extends [NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Currently, there's no test case that verifies this conditional [Union] extends [NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray]. All tests continue to pass even if the conditional is removed.


A test like the following is needed to verify the above conditional:

expectType<Map<string, string> | Set<string>>({} as SharedUnionFields<Map<string, string> | Set<string>>);

Copy link
Collaborator

@som-sm som-sm Dec 4, 2024

Choose a reason for hiding this comment

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

Also, is this the intended behaviour?

type T = SharedUnionFields<RegExp | {test: 1} | {test: 2}>;

T is currently of the following type:

{ test: 1 | 2 | ((string: string) => boolean); }

Feels like one of these behaviours might be better than the existing behaviour in scenarios like these:

  1. We simply return back the input type, without any manipulation, like we'd do for RegExp | Date.
    RegExp | {test: 1} | {test: 2}
  2. Or, we can leave RegExp as is, and resolve the remaining members
    RegExp | {test: 1 | 2}

@Emiyaaaaa WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. Or, we can leave RegExp as is, and resolve the remaining members
    RegExp | {test: 1 | 2}

This is better

@category Object
@category Union
*/
export type SharedUnionFields<Union> =
Copy link
Collaborator

Choose a reason for hiding this comment

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

The implementation of this type could be refactored to simply this:

export type SharedUnionFields<Union> = [Union] extends [
	NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray,
]
	? Union
	: Pick<Union, keyof Union>;

This seems to handle all the scenarios.

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.

3 participants