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

Extracting return types from functions while mapping types? #15763

Closed
JasonKleban opened this issue May 11, 2017 · 2 comments
Closed

Extracting return types from functions while mapping types? #15763

JasonKleban opened this issue May 11, 2017 · 2 comments
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@JasonKleban
Copy link

JasonKleban commented May 11, 2017

I don't recognize any examples of this in the docs. Can we map to a type that holds the return types of key-indexed functions? Is there a syntax for that?

var ex = {
    One<S>(x : S) { return x; }
    
    Two<T>(y : T) { return "foo"; }
    
    Three() { return false; }
}

 // Bad because all return types would have to be the same `T`
type BadMappedOutputs<T, O extends { [k : string] : () => T }> = {
    [K in keyof O] : T
}

// Something like this?
type MappedOutputs<O> = {
    <T>[K in keyof O where O[K] extends () => T] : T
}

var baz = MappedOutputs<typeof ex>;

baz.One; // any
baz.Two; // string
baz.Three; // boolean
@aluanhaddad
Copy link
Contributor

I believe this is a duplicate of #6606.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

You should be able to get this working today with Conditional Types

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;

type MappedOutputs<T> = {
    [P in keyof T]: ReturnType<T[P]>
}

@mhegazy mhegazy closed this as completed Feb 8, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants