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

Proposal: SubType #22

Closed
rifler opened this issue Apr 6, 2019 · 11 comments
Closed

Proposal: SubType #22

rifler opened this issue Apr 6, 2019 · 11 comments

Comments

@rifler
Copy link

rifler commented Apr 6, 2019

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

Examples:

var object = {
    a: 1,
    b: true,
    c: () => null,
    d: () => null,
};

type JustMethods = SubType<typeof object, (...args: unknown[]) => unknown>;
// { c: () => null, d: () => null }

JustMethods from #4 can be realized via SubType. But, as I understand, JustProps can't. Maybe it will be possible after microsoft/TypeScript#29317

@rifler
Copy link
Author

rifler commented Apr 13, 2019

Hi there

@sindresorhus
Copy link
Owner

sindresorhus commented Apr 19, 2019

Can you elaborate on some real-world scenarios when this type would be useful? Preferably point to real code where it's used or where it could be used.

This seems like a more powerful Extract<T, U> helper. So the naming should probably be something similar.

@kaleb
Copy link

kaleb commented Jun 21, 2019

I have used a similar type. One use-case that I have is that I have a function that takes an object, and the key of a method to call. I only want the keys of the methods. This will work for that.

@sindresorhus
Copy link
Owner

@kaleb Would be great if you could include or link to some actual code using this pattern.

@sindresorhus
Copy link
Owner

@fabiospampinato @kainiedziela @WORMSS @uyeong @dylang Could use you feedback :)

@fabiospampinato
Copy link
Contributor

This seems like a more powerful Extract<T, U> helper. So the naming should probably be something similar.

Maybe ExtractProps? It's basically just like Extract but filters T's props rather than T itself.

It looks like a pretty powerful type, but I'm not sure in what scenario I would use it 🤷‍♂.

For instance just to give a quick example I'd personally rather write this:

type Options = {
	foo: number,
	bar: boolean,
	events: {
      onFoo: Function,
      onBar: Function
    }
};

type JustMethods = Options['events'];

Than this:

type Options = {
	foo: number,
	bar: boolean,
    onFoo: Function,
    onBar: Function
};

type JustMethods = SubType<Options, Function>;

There are definitely scenarios where I would use Pick, picking just a few specific props, but picking those props by their type sounds a bit weird to me right now, and perhaps error-prone too: what if at a later time I add another matching prop to the object but I don't actually want it to be included in the sub type?

@kainiedziela
Copy link
Contributor

Maybe ExtractProps?

I think that could confuse users thinking it extracts all props and omits methods.

I agree with the rest of Fabio's points. This seems like a powerful type that would be prone to misuse.

@sindresorhus
Copy link
Owner

Thanks for suggesting this type. However, I have decided to pass on it for reasons outlined in the discussion here.

@RubenVerg
Copy link

This does what I'd call a type filter, right? Or am I misunderstanding? Pick all props in Base (/methods, whatever) that extend Condition, that is. Just trying to see if I have any use-case ;)

@mattdiamond
Copy link

@sindresorhus I'm pretty sure the utility being described in this issue is the same as ConditionalPick, which type-fest includes... you might want to consider removing this from the list of declined types to avoid confusion.

@sindresorhus
Copy link
Owner

e05be93

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

No branches or pull requests

7 participants