Skip to main content

Automatic return type inference

What type inference is

Type inference is when TypeScript determines the type of a variable, parameter, or function result on its own, by analyzing the return expressions inside the function body.

That is, if you did not specify a type after ):, TypeScript will try to guess it itself.


Example 1. A simple case

javascript
function add(a: number, b: number) { return a + b; }

TypeScript analyzes return a + b; and sees:

  • a: number
  • b: number
  • the result of the operation a + b -> number

So it infers the type itself:

javascript
// automatically function add(a: number, b: number): number

Example 2. Returning a string

javascript
function greet(name: string) { return `Hello, ${name}!`; }

TypeScript sees that return always returns a template string -> type string.

Inference:

javascript
// automatically function greet(name: string): string

Example 3. Returning different types (a union)

If there are several return branches with different types, TS infers a union type (typeA | typeB):

javascript
function maybeGetNumber(flag: boolean) { if (flag) { return 42; // number } else { return "none"; // string } }

Inference:

javascript
// automatically function maybeGetNumber(flag: boolean): number | string

Example 4. Returning an object

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

TypeScript infers the type from the structure of the returned object:

javascript
// automatically function makeUser(name: string, age: number): { name: string; age: number }

Example 5. Async functions

If a function is declared as async, TypeScript wraps the result in Promise<...>.

javascript
async function getUser() { return { id: 1, name: "Tim" }; }

Inference:

javascript
// automatically async function getUser(): Promise<{ id: number; name: string }>

Example 6. A function without return

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

No return -> TypeScript infers the type void.

Inference:

javascript
// automatically function log(message: string): void

Example 7. A complex return -> TypeScript can get it wrong

Sometimes the inferred type can be too broad. For example:

javascript
function process(value: any) { if (typeof value === "string") { return value.toUpperCase(); } return null; }

TS infers the type:

javascript
function process(value: any): string | null

This is correct, but if you later change the code, TypeScript will not always notice a mismatch in the logic. That's why for public functions and APIs it is better to specify the type explicitly.


Example 8. Generics + type inference

TypeScript can infer the generic parameter <T> and automatically determine the returned value:

javascript
function identity<T>(value: T) { return value; } const a = identity(10); // T = number -> the function returns number const b = identity("hello"); // T = string -> the function returns string

TS "substitutes" the type T from the call context.


How TypeScript performs inference (simplified)

  1. Scans all return statements in the function body
  2. Determines the type of each return expression
  3. Finds the common supertype (or a union, if there are several types)
  4. Assigns this type to the function as its return type

When it's worth specifying the type explicitly

Although TypeScript can infer types, an explicit annotation is often better, especially if:

SituationWhy it's better to specify it
Public functions / APIsImproves predictability and autocomplete
Complex logic with multiple return statementsHelps avoid unobvious union types
Async functionsIt's clear that a Promise<T> is returned
Generic functionsHelps constrain the possible types
Logging / utilitiesEasier to maintain and document the code

Summary

ScenarioInferred type
return 5number
return "hi"string
return { name: "Tim" }{ name: string }
No returnvoid
async functionPromise<...>
Different returnsA union type (`A

The main rule:

TypeScript automatically infers the type from return, but in public, reusable functions it's better to specify the return type explicitly - it makes the code more reliable and clearer.

Short Answer

Interview ready
Premium

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