In TypeScript, what exactly is type '{}'
and how does it relate to other built-in types?
For example, the last line of the following example gives Type '{}' is not assignable to type 'number'
, and I am not completely clear on what type {}
is in this context, or indeed how it comes about:
// A function taking no arguments and returning T
type NoArgsFn<T> = () => T;
// An instance of NoArgsFn<number>
function get_the_answer(): number {
return 42;
}
// Call the supplied function and return its value
function call<T, Fn extends NoArgsFn<T>>(fn: Fn): T {
return fn();
}
// Expect this to be equivalent to `let the_answer: number = 42', but
// instead get "Type '{}' is not assignable to type 'number'"
let the_answer: number = call(get_the_answer);