Imagine having the following function:
let getData = () => {
return {
name: 'John',
age: 2,
isAsian: true
}
}
Is there a way to specify a variable's type as the return type of getData
, without assigning it to getData
directly?
For instance, is there a syntax like:
var data: getData
But this approach doesn't work because it sets the type as the function itself, not its return value. (Edit: This would result in the error: Cannot find name 'getData'
)
var data = getData()
would assign the correct type to data
as
{name: string, age: number, isAsian: boolean}
, but this doesn't meet my requirements.
There is a relevant discussion here that may offer insights: Obtaining the return type of a function
However, this doesn't seem to provide the exact solution I'm seeking. I am not able to adopt Typescript 2.8 yet due to restrictions with Angular and the CLI. I believe the alternative methods discussed in the linked Stack Overflow question won't help me achieve dynamic use of my function's return type.