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

Type for arbitrary number of function parameters of specified types #4338

Closed
panuhorsmalahti opened this issue Aug 17, 2015 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@panuhorsmalahti
Copy link

I'd like to have a type for a function which takes in zero or more parameters, and then use the type for the parameters, e.g.

// Note: the syntax below of course doesn't do what I'm looking for, something like (params: ...args: T) would be closer
let f = (params: T) => (params2: T) => 0;

// these should be legal:
f(5)(3);
f("foo")("bar");
f(5, "foo")(3, "bar");

// these should be illegal
f(5)("bar");
f("foo")(3);
f(5, 3)("foo", "bar");

This is useful in e.g. typing the spread function here:
DefinitelyTyped/DefinitelyTyped@3eb67c9

I need to use the ...args: any[], which is not quite what I'm looking for.

In essence, the type I want to use is [A?, B?, C?, ...], or maybe something like Params<T> if such an interface would exist.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Aug 17, 2015
@JsonFreeman
Copy link
Contributor

Looks like you want something that allows a variable number of parameters with arbitrary types, but then captures them so they can be repeated in another signature. I think you may find a relevant discussion at #3622 (comment) and #3870.

@benliddicott
Copy link

Do you actually need an infinite number of parameters? If in practice you top out at something like ten or twenty then this might be a good pattern:

    function f<T1>(a1: T1): (a1: T1) => any;
    function f<T1, T2>(a1: T1, a2: T2): (a1: T1, a2: T2) => any;
    function f<T1, T2, T3>(a1: T1, a2: T2, a3: T3): (a1: T1, a2: T2, a3: T3) => any;
    function f<T1, T2, T3, T4>(a1: T1, a2: T2, a3: T3, a4: T4): (a1: T1, a2: T2, a3: T3, a4: T4) => any;
    function f<T1, T2, T3, T4, T5>(a1: T1, a2: T2, a3: T3, a4: T4, a5: T5): (a1: T1, a2: T2, a3: T3, a4: T4, a5: T5) => any;
   // Continue the theme until you are sure you have met requirements. Maybe ten or twenty arguments?
    function f(...params: any[]):any {

    }


It's flexible, doesn't require new syntax, and works today.

@JsonFreeman
Copy link
Contributor

That is indeed the recommended pattern to use today.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2016

looks like a duplicate of #1773

@mhegazy mhegazy closed this as completed Feb 22, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 22, 2016
@RyanCavanaugh RyanCavanaugh removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Feb 22, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants