Type queries for functions

Topics: Language Specification
Oct 29, 2013 at 7:12 PM
Edited Oct 29, 2013 at 10:36 PM

From the TypeScript specification, "Type queries are useful for capturing anonymous types that are generated by various constructs such as object literals, function declarations, and module declarations."

Is it possible to capture the inferred output type of a function?

var f = function() {
  return {x: 1};
var y = f();
I'd like to refer to the type of y without having to invoke f. Type queries seemed promising, but I can't seem to write the right one.

Nov 4, 2013 at 4:37 PM
It's hacky, but the expression false && f() will have the type of f() without invoking it.
Nov 4, 2013 at 9:01 PM
Maybe a new keyword `returnof` would be useful - where returnof works like typeof, expect returnof refers to the type returned by the specified function, rather than the function type.

Andrew Gaspar