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

Inferring function return type then use it instead of function itself #13159

Closed
pocesar opened this issue Dec 24, 2016 · 3 comments
Closed

Inferring function return type then use it instead of function itself #13159

pocesar opened this issue Dec 24, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@pocesar
Copy link

pocesar commented Dec 24, 2016

Not sure if this is already possible with Typescript or it's being discussed (I can't even think about the keywords for the search).

Suppose I have a type defines a function that returns a generic:

    type Function1<T1, R> = (t1: T1) => R;
    interface SomeWrapper<T>{ 
        index: T;
    }

I then accept it as a parameter in a function, inferring it's return value:

    export function funct<P, A1>(
        type: string,
        cb: Function1<A1, P>
    ): Function1<A1, SomeWrapper<P>>;

when funct is called, it then returns a new 'partial' function that returns a wrapped object:

const f1 = funct('string', (param1: string) => ({ param1 }))
// returns a function that when that f1('') => { index: { param1: '' } }, of type Function1

I want to pass this f1 variable to another function, and it'll know the parameter type by inferring it (again):

   export function useFunct<V, A1, F extends Function1<A1, V>>(type: F, reduce: (current: F) => V): V; 

then when doing:

useFunct(f1, (param) => {
  // expect param to be of type string inferred from f1
  // it's of course a function, and not the value of the function
  return {
    param
  }
})

is it possible or it needs an implementation? if not, I think it would be like returnOf keyword (like keyof)

   export function useFunct<V, A1, F extends Function1<A1, V>>(type: F, reduce: (current: returnOf F) => V): V; 
@mhegazy
Copy link
Contributor

mhegazy commented Dec 26, 2016

Possibly a duplicate of #10247

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 26, 2016
@pocesar
Copy link
Author

pocesar commented Dec 26, 2016

well I'm most interested in using the return value of the function, the function parameters can be already be inferred (with the interface and multiple arguments generics), it's a simpler case I think, an extra keyword, not HKTs, like typeof works on plain function definitions, and you get it's "type" so you can put it on an interface

EDIT: now that I'm writing this, I remembered I wrote about something like this in #12497, getting the result of the constructor is the same goal, so it would make things really easy for typed constructors / factories as well

@mhegazy
Copy link
Contributor

mhegazy commented Dec 26, 2016

#6606 tracks getting the return type of a function/constructor.

@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

3 participants