Sometimes, we want to get a function’s return value type with TypeScript.
In this article, we’ll look at how to get a function’s return value type with TypeScript.
How to get a function’s return value type with TypeScript?
To get a function’s return value type with TypeScript, we can use the ReturnType
type.
For instance, we write
const createPerson = () => ({
firstName: "John",
lastName: "Doe",
});
type Person = ReturnType<typeof createPerson>;
to get the type of the createPerson
function with typeof createPerson
.
Then we can get the return type of the function with ReturnType<typeof createPerson>
and assign the type to the Person
alias.
Conclusion
To get a function’s return value type with TypeScript, we can use the ReturnType
type.