Skip to content

Extracting return types from functions while mapping types? #15763

Closed
@JasonKleban

Description

@JasonKleban

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions