-
-
Notifications
You must be signed in to change notification settings - Fork 569
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
Add SharedUnionFields
type
#994
Conversation
Hi @sindresorhus, would it be possible to add me to the funding.yml? Thanks a lot! |
Of course: ca449dc |
There was a problem hiding this 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] |
There was a problem hiding this comment.
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>>);
There was a problem hiding this comment.
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:
- We simply return back the input type, without any manipulation, like we'd do for
RegExp | Date
.RegExp | {test: 1} | {test: 2}
- Or, we can leave
RegExp
as is, and resolve the remaining membersRegExp | {test: 1 | 2}
@Emiyaaaaa WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Or, we can leave
RegExp
as is, and resolve the remaining membersRegExp | {test: 1 | 2}
This is better
@category Object | ||
@category Union | ||
*/ | ||
export type SharedUnionFields<Union> = |
There was a problem hiding this comment.
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.
Simple version of
SharedUnionFieldsDeep