ReturnType
This lesson explains the ReturnType mapped type.
We'll cover the following...
Extracting the return type of a function
In some cases, you may want to extract the returned type of a function. TypeScript comes with a ReturnType
mapping function that gives you this information.
For example, if you have a function that returns a string
, you can use ReturnType<yourFunction>
and it will return the type string
. The following code on line 5 assigns to a variable, the type which is a string because the return type of the function getName
is a string
.
Press + to interact
function getName(): string {return "Name";}type FunctionType = ReturnType<typeof getName>;const varX:FunctionType = "This is a string";console.log(varX);
ReturnType
becomes important in case the ...