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

PickType and OmitType #50

Closed
ozum opened this issue Jan 9, 2019 · 7 comments · Fixed by #79
Closed

PickType and OmitType #50

ozum opened this issue Jan 9, 2019 · 7 comments · Fixed by #79

Comments

@ozum
Copy link
Contributor

ozum commented Jan 9, 2019

Hi,

Could you please add PickType and OmitType types similar to Pick and Omit. It would be helpful. They pick and omit properties based on types of values instead of key names:

I changed names to make it more compatible with utility-types. You can change if you have a more appropriate name in your mind.

Types

// From `Base` pick a set of properties with types of `Condition`
type PickType<Base, Condition> = Pick<Base, { [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base]>;

// From `Base` remove a set of properties with types of `Condition`
type OmitType<Base, Condition> = Pick<Base, { [Key in keyof Base]: Base[Key] extends Condition ? never : Key }[keyof Base]>;

Usage

type Props = { name: string; age: number; visible: boolean };

type SomeProps = PickType<Props, string | number>;
// Expect: { name: string; age: number }

type OtherProps = OmitType<Props, string | number>;
// Expect: { visible: boolean }

Types are not developed by me. I saw them on a blog post by Piotr Lewandowski and used in several projects.
Credits: https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c

Thanks,

@piotrwitek
Copy link
Owner

piotrwitek commented Jan 11, 2019

I like this types, they are definitely useful.

For the name I would propose:

  • PickByValue
  • OmitByValue

I choose this name because regular pick is picking by key, and PickByValue is picking by the value, check the example below:

Examples:

type PickByValue<Base, Condition> = Pick<
  Base,
  { [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base]
>;

type Props2 = { name: string; 0: boolean };

type SomeProps1 = Pick<Props2, 0>;
// Expect: { 0: boolean }

type SomeProps2 = PickByValue<Props2, boolean>;
// Expect: { 0: boolean }

Accepting PR's.

piotrwitek pushed a commit that referenced this issue Jan 14, 2019
This PR adds `PickByValue` and `OmitByValue` per #50.
@piotrwitek piotrwitek added the blocked by limitations blocked by TypeScript limitations label Jan 14, 2019
@piotrwitek
Copy link
Owner

piotrwitek commented Jan 14, 2019

It's merged but I'm blocking the release for the moment. Mainly because there are few other API proposals and until all of them are sorted out I don't want to make a mistake and introduce some breaking changes in the process.

@ProdigySim
Copy link

What happened to these? It looks like they got merged, but then they were removed from index.ts... and are now difficult to import.

@zepatrik
Copy link

currently:

import { PickByValue, OmitByValue } from 'utility-types/dist/mapped-types'

@piotrwitek
Copy link
Owner

I'll have some time during weekend, so hopefully I can finish the proposal for this API and make it public

@piotrwitek piotrwitek added needs funding and removed accepting PRs blocked by limitations blocked by TypeScript limitations labels Apr 25, 2019
@IssueHuntBot
Copy link

@BoostIO has funded $30.00 to this issue.


piotrwitek added a commit that referenced this issue Apr 28, 2019
piotrwitek added a commit that referenced this issue Apr 28, 2019
- Added RequiredKeys and OptionalKeys #53
- Added OmitByValue and PickByValue to the public API #50 
- Added OmitByValueExact and PickByValueExact #59
@IssueHuntBot
Copy link

@piotrwitek has rewarded $21.00 to @piotrwitek. See it on IssueHunt

  • 💰 Total deposit: $30.00
  • 🎉 Repository reward(20%): $6.00
  • 🔧 Service fee(10%): $3.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants