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 query for a result of a function call #6239

Closed
Strate opened this issue Dec 24, 2015 · 11 comments
Closed

Type query for a result of a function call #6239

Strate opened this issue Dec 24, 2015 · 11 comments
Labels
Duplicate An existing issue was already created

Comments

@Strate
Copy link

Strate commented Dec 24, 2015

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

@Strate
Copy link
Author

Strate commented Dec 24, 2015

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

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Dec 25, 2015
@DanielRosenwasser
Copy link
Member

This comment is related: #2710 (comment)

@yortus
Copy link
Contributor

yortus commented Dec 25, 2015

Duplicate of #4233?

@tinganho
Copy link
Contributor

I have also encountered this problem many times.

@zpdDG4gta8XKpMCd
Copy link

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.

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

Might be related #5959

@Strate
Copy link
Author

Strate commented Dec 25, 2015

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

let a: typeof fn<number>()

to get {value: number}

@zpdDG4gta8XKpMCd
Copy link

@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"

@Strate
Copy link
Author

Strate commented Dec 25, 2015

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

@Strate Strate changed the title Type query for function's result Type query for a result of a function call Dec 25, 2015
@tinganho
Copy link
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

@yortus
Copy link
Contributor

yortus commented Dec 25, 2015

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

@yortus
Copy link
Contributor

yortus commented Jan 25, 2016

Proposal + implementation that covers this: #6606

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Jan 25, 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

6 participants