Skip to main content

Return value type

General syntax

The return type is specified after the parameter list (after )):

javascript
function functionName(parameters): ReturnType { // ... }

Example 1. Returning a number

javascript
function sum(a: number, b: number): number { return a + b; }
  • parameters -> a, b (type number)

  • return value -> number

  • if you try to return something other than a number, TypeScript throws an error:

    javascript
    return "text"; // Error

Example 2. Returning a string

javascript
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:

javascript
function logMessage(message: string): void { console.log(message); }

Technically, such a function returns undefined, but void tells TypeScript: "the return value is not used".


Example 4. Returning boolean

javascript
function isAdult(age: number): boolean { return age >= 18; }

Example 5. Returning an object

javascript
function createUser(name: string, age: number): { name: string; age: number } { return { name, age }; }

You can extract the type separately for readability:

javascript
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.

javascript
function fail(message: string): never { throw new Error(message); }

or

javascript
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>:

javascript
async function fetchUser(): Promise<{ name: string }> { return { name: "Tim" }; }

or

javascript
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:

javascript
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

javascript
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 typeWhen it's usedExample
numberReturning a numbersum(a, b): number
stringReturning textgreet(name): string
booleanReturning a boolean valueisAdult(age): boolean
voidNo return valuelog(): void
neverNever finishesthrowError(): never
Promise<T>Async resultfetchData(): Promise<User>
T, UGeneric typeidentity<T>(value: T): T

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.