这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix O.P.Pick to distribute over unions #56

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

Merged
merged 4 commits into from
Sep 30, 2019

Conversation

mcpower
Copy link
Contributor

@mcpower mcpower commented Sep 30, 2019

🎁 Pull Request

  • Used a clear / meaningful title for this pull request
  • Tested the changes in your own code (on your projects)
  • Added / Edited tests to reflect changes (tst folder)
  • Have read the Contributing part of the Readme
  • Passed npm test

Fixes

Having a non-object in a union as a property breaks O.P.Pick for any properties in the union.

For example, note how PartOneAndTwo incorrectly picks the nested object:

// {a: 'a'}
type PartOne = O.P.Pick<{a: 'a', b: 'b'}, ['a', 'b']>;
// {a: {b: 'ab'}}
type PartTwo = O.P.Pick<{a: {b: 'ab', c: 'ac'}, b: 'b'}, ['a', 'b']>;
// {a: 'a' | {b: 'ab', c: 'ac'}}
type PartOneAndTwo = O.P.Pick<{a: {b: 'ab', c: 'ac'} | 'a', b: 'b'}, ['a', 'b']>;

This PR fixes PartOneAndTwo to evaluate to {a: 'a' | {b: 'ab'}}.

Why have you made changes?

To fix the issues above.

What changes have you made?

  • Added an infer to O.P.Pick, making the conditional type distributive. This removes the need to cast Picked[K] to an object and distributes Picked[K] over all types.

What tests have you updated?

  • added a new test for O.P.Pick in tst/Object.ts

Is there any breaking changes?

  • Yes, I changed the public API & documented it
  • Yes, I changed existing tests
  • No, I added to the public API & documented it
  • No, I added to the existing tests
  • I don't know

Anything else worth mentioning?

Working with advanced types in TypeScript is really fun 😁

@mcpower mcpower requested a review from millsp as a code owner September 30, 2019 11:32
: _Pick<Picked[K] & {}, Path, Next<I>> // 1-0: Continue diving
: Picked[K] // 0: Pick property
[K in keyof Picked]:
Picked[K] extends infer Prop // Needed for the below to be distributive
Copy link
Owner

Choose a reason for hiding this comment

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

interesting, thanks, never thought of this 👍

Copy link
Owner

Choose a reason for hiding this comment

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

clever

Copy link
Owner

@millsp millsp left a comment

Choose a reason for hiding this comment

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

Hi,

This is very clever. Would you mind doing this for the other P types as well?
I have a policy for doing upgrades at once, to keep the progress even accross the types.

I'll propagate this change to the rest of the library (for all the other recursive types).

Thanks for the idea, it will help to ligthen some of the recursive types by 10-15%.

@mcpower mcpower requested a review from millsp September 30, 2019 13:08
@mcpower
Copy link
Contributor Author

mcpower commented Sep 30, 2019

I should have implemented this for all the P types - the existing tests still pass and the new tests seem sensible enough. Please review 😄

@millsp millsp merged commit 56a0131 into millsp:master Sep 30, 2019
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