Return value type
General syntax
The return type is specified after the parameter list (after )):
function functionName(parameters): ReturnType {
// ...
}Example 1. Returning a number
function sum(a: number, b: number): number {
return a + b;
}-
parameters ->
a,b(typenumber) -
return value ->
number -
if you try to return something other than a number, TypeScript throws an error:
javascriptreturn "text"; // Error
Example 2. Returning a string
function greet(name: string): string {
return `Hello, ${name}!`;
}Example 3. Returning void (the function returns nothing)
If a function returns nothing (it only performs an action),
the void type is used:
function logMessage(message: string): void {
console.log(message);
}Technically, such a function returns
undefined, butvoidtells TypeScript: "the return value is not used".
Example 4. Returning boolean
function isAdult(age: number): boolean {
return age >= 18;
}Example 5. Returning an object
function createUser(name: string, age: number): { name: string; age: number } {
return { name, age };
}You can extract the type separately for readability:
type User = { name: string; age: number };
function createUser(name: string, age: number): User {
return { name, age };
}Example 6. Returning never (the function never finishes)
Used if a function will never return a value - for example, it throws an exception or runs forever.
function fail(message: string): never {
throw new Error(message);
}or
function loopForever(): never {
while (true) {}
}Example 7. Returning Promise<T> (async functions)
Async functions return a promise.
The return type is specified as Promise<Type>:
async function fetchUser(): Promise<{ name: string }> {
return { name: "Tim" };
}or
function fetchData(): Promise<string> {
return new Promise((resolve) => resolve("OK"));
}Example 8. TypeScript can infer the type automatically
If you didn't specify the type explicitly, TypeScript tries to infer it from return:
function add(a: number, b: number) {
return a + b; // TS determines the type on its own: number
}However, it's better to specify the type explicitly, especially in public APIs - it improves predictability and autocomplete.
"All together" example
type User = { id: number; name: string };
function getUser(id: number): User | null {
if (id === 1) {
return { id: 1, name: "Tim" };
}
return null;
}
function printUser(user: User | null): void {
if (!user) {
console.log("User not found");
return;
}
console.log(`User: ${user.name}`);
}Summary
| Return type | When it's used | Example |
|---|---|---|
number | Returning a number | sum(a, b): number |
string | Returning text | greet(name): string |
boolean | Returning a boolean value | isAdult(age): boolean |
void | No return value | log(): void |
never | Never finishes | throwError(): never |
Promise<T> | Async result | fetchData(): Promise<User> |
T, U | Generic type | identity<T>(value: T): T |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.