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

Idea: Type Unpackers for Function Parameters #22

Closed
fbartho opened this issue Jun 14, 2018 · 11 comments · Fixed by #24
Closed

Idea: Type Unpackers for Function Parameters #22

fbartho opened this issue Jun 14, 2018 · 11 comments · Fixed by #24

Comments

@fbartho
Copy link
Contributor

fbartho commented Jun 14, 2018

Because of TypeScript 2.8 Conditional Types, we can now do the following:

type Param0<Func> = Func extends (a: infer T, ...args: any[]) => any
	? T
	: never;
type T0 = Param0<() => void>; // {}
type T1 = Param0<(a: number) => void>; // number
type T2 = Param0<(a: string[]) => void>; // string[]

Issue for discussion!

Questions:

  • Naming scheme?
  • Multiple Parameter Unpacks at the same time?

See also ReturnType<Func>

@pelotom
Copy link
Owner

pelotom commented Jun 14, 2018

Great idea! What about just one operator which returns a tuple of types?

type ParamTypes<F extends Function> =
  F extends () => any ? {} :
  F extends (p0: infer P0) => any ? [P0] :
  F extends (p0: infer P0, p1: infer P1) => any ? [P0, P1] :
  F extends (p0: infer P0, p1: infer P1, p2: infer P2) => any ? [P0, P1, P2] :
  // ...
  never;

type T0 = ParamTypes<() => void>; // {}
type T1 = ParamTypes<(a: number) => void>; // [number]
type T2 = ParamTypes<(a: string, b: boolean) => void>; // [string, boolean]

Then you can further access particular indexes as you please, e.g.

type Third = ParamTypes<typeof f>[2];

I used ParamTypes for consistency with ReturnType; I could also be convinced Parameters or Params is a good name...

@kourge
Copy link

kourge commented Jun 14, 2018

ArgumentsType seems more consistent, in line with arguments as well as (...args: any[]).

@fbartho
Copy link
Contributor Author

fbartho commented Jun 14, 2018

@kourge That's a great point. Consistent options:

  • ArgumentsType
  • ArgumentTypes
  • ArgumentType (plural is implied by operator's type-tuple return?).

@pelotom Is there a performance concern re: unpacking the whole array by default? If people are mostly going to access a single parameter, maybe we should consider that?

@pelotom
Copy link
Owner

pelotom commented Jun 14, 2018

ArgumentsType seems more consistent, in line with arguments as well as (...args: any[]).

Parameters are the declared inputs of a function signature, arguments are the runtime objects passed to a function:

https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter

@pelotom Is there a performance concern re: unpacking the whole array by default? If people are mostly going to access a single parameter, maybe we should consider that?

I really doubt there's much performance difference.

@pelotom
Copy link
Owner

pelotom commented Jun 14, 2018

On second thought you may be right about performance being a concern... I agree it might be better to play it safe.

@fbartho
Copy link
Contributor Author

fbartho commented Jun 15, 2018

@pelotm I’d be happy with N short helpers, and also providing an up-to-N batch unpacker. (With a performance warning)

Then naming wise, should it be:

  • ParamN
  • ParamTypeN
  • ParameterN
  • ParameterTypeN

(Where N is a number) or should it be something else?

How many N should we provide?

@fbartho
Copy link
Contributor Author

fbartho commented Jun 15, 2018

(I think the final name of the short form will influence our long form name)

@pelotom
Copy link
Owner

pelotom commented Jun 15, 2018

I think I'd actually vote ParamN (what you originally had).

fbartho added a commit to fbartho/type-zoo that referenced this issue Jul 10, 2018
@fbartho
Copy link
Contributor Author

fbartho commented Jul 10, 2018

Implemented in #24

pelotom pushed a commit that referenced this issue Jul 10, 2018
* Update Dependencies - yarn.lock

* Implement ParamN & ParamTypes

for #22
@chocolateboy
Copy link

Is this the same as the built-in Parameters type (which doesn't have a restriction on the number of parameters)?

https://github.com/microsoft/TypeScript/blob/eca8957430bf52cfdfa2bfe03c9431682da6a558/lib/lib.es5.d.ts#L1514

If so, does it have the same limitation (only returns the first overloaded parameters)?

@fbartho
Copy link
Contributor Author

fbartho commented Jan 10, 2021

@chocolateboy I believe these are equivalent implementations. The built-in one didn't exist when we added this one, as far as I know.

As such I expect it to be subject to the same limits. But you should test it and let us know!

We don't use many overridden method definitions in our codebase, because the syntax could be confusing.

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 a pull request may close this issue.

4 participants