Course contentsShow
← TypeScript
Lesson 236 of 1,7966. FunctionsFree lesson

Function Type Syntax

Learn the basic syntax for annotating function parameter and return types in TypeScript.

Function Type Syntax

What you'll learn: How to annotate the types of function parameters and return values in TypeScript.

Why Functions Need Type Annotations

Functions are the building blocks of JavaScript, and in TypeScript, you want to specify what types of data go in and what type comes out. Unlike variables where TypeScript can often infer types, function parameters need explicit annotations—TypeScript can't guess what you intend to pass.

Basic Function Type Syntax

Here's the pattern:

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

Parameter annotation: name: string tells TypeScript this function accepts a string argument.

Return type annotation: : string after the parentheses declares what the function returns.

Multiple Parameters

Each parameter gets its own type annotation:

function add(x: number, y: number): number {
  return x + y;
}

Return Type Inference

While you can annotate return types, TypeScript often infers them:

function multiply(a: number, b: number) {
  return a * b;  // TypeScript infers return type: number
}

However, explicitly annotating return types is considered a best practice for documentation and catching errors early.

Functions Returning Nothing

When a function doesn't return a value, use void:

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

Key Takeaway: TypeScript function type syntax annotates parameters with : Type after each parameter name and return types with : Type after the parameter list—this creates a type-safe contract for what goes in and what comes out.