Skip to content

Type query for a result of a function call #6239

Description

Is it possible to use typeof type query operator to get function's return type?

Activity

  1. Strate commented on Dec 24, 2015

    @Strate
    Author

    Seems that no. It would be great to do something like:

    declare var do: (arg: string) => number
    
    let a: typeof do // a now has (arg: string) => number type
    let b: typeof do() // b now has number type
    
  2. DanielRosenwasser commented on Dec 25, 2015

    @DanielRosenwasser
    Member

    This comment is related: #2710 (comment)

  3. yortus commented on Dec 25, 2015

    @yortus
    Contributor

    Duplicate of #4233?

  4. tinganho commented on Dec 25, 2015

    @tinganho
    Contributor

    I have also encountered this problem many times.

  5. zpdDG4gta8XKpMCd commented on Dec 25, 2015

    @zpdDG4gta8XKpMCd

    What would be the inferred type in the following situation?

    function fn<a>(value: a) : { value: a; } { return { value: value }; }
    let a: typeof fn;

    If the answer is {value: {}} (assuming the current state of affairs) then such feature is useless.

    Daniel Rosenwasser (@DanielRosenwasser), how feasible is it to allow variables to hold unresolved its own declared (not from the context) type parameters?

    Might be related #5959

  6. Strate commented on Dec 25, 2015

    @Strate
    Author

    I think answer should be an error about required type argument, and developer should write

    let a: typeof fn<number>()

    to get {value: number}

  7. zpdDG4gta8XKpMCd commented on Dec 25, 2015

    @zpdDG4gta8XKpMCd

    Artur Eshenbrener (@Strate)
    this is pure syntax sugar over

    let b = fn<number>();
    let a : typeof b;

    if this is what you are looking for then it might be better phrased as "Type query for a result of a function call"

  8. Strate commented on Dec 25, 2015

    @Strate
    Author

    You are right, I'm gonna to change the caption :)

  9. changed the title [-]Type query for function's result[/-] [+]Type query for a result of a function call[/+] on Dec 25, 2015
  10. tinganho commented on Dec 25, 2015

    @tinganho
    Contributor

    I'm wondering how this would handle overloaded functions? I guess one must pass in the type of the arguments to let it overload correctly the return type.

    function f(a: string): string;
    function f(a: string, b: number): boolean;
    function f(a: string, b?: string | number, c?: string): boolean | string {
        return true;
    }
    
    typeof f(string, number) // boolean

    Isn't ellipses ... a better syntax for not overloaded functions? Because only f() (no arguments) doesn't corresponds to the function signature f(a: string) (with one argument).

    function f(a: string): boolean;
    typeof f(...) // boolean
  11. yortus commented on Dec 25, 2015

    @yortus
    Contributor

    Tingan Ho (@tinganho) #4233 takes care of that - just use any valid expression - like typeof f("", 0)

  12. yortus commented on Jan 25, 2016

    @yortus
    Contributor

    Proposal + implementation that covers this: #6606

  13. added
    DuplicateAn existing issue was already created
    and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    SuggestionAn idea for TypeScript
    on Jan 25, 2016
  14. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions