0Pricing
TypeScript Academy · Lesson

Parameter and Return Type Annotations

Add types to function inputs and outputs.

Parameter and Return Type Annotations is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Typing function parameters and return values is one of the most important things you can do in TypeScript. It documents the contract and prevents bugs at call sites.

Parameter Types

Add a colon and type after each parameter name to annotate it.
function greet(name: string): string {
  return 'Hello, ' + name;
}

Return Type Annotations

Annotate the return type after the closing parenthesis with a colon and type. TypeScript verifies the function actually returns that type.
function add(a: number, b: number): number {
  return a + b;
}

Inferring Return Types

TypeScript can infer return types from the return statement. Explicit annotations are optional but improve readability and catch refactoring bugs.
function double(n: number) {
  return n * 2; // TypeScript infers: number
}

Arrow Functions

Arrow functions use the same annotation syntax. The return type goes after the parameter list.
const multiply = (a: number, b: number): number => a * b;

void Return Type

Functions that do not return a value use the `void` return type.
function logMessage(msg: string): void {
  console.log(msg);
}

Function Type Expressions

You can describe a function's shape as a type using a function type expression and assign it to a variable.
type MathFn = (a: number, b: number) => number;
const add: MathFn = (a, b) => a + b;

Multiple Return Types with Union

A function may return different types depending on logic. Use a union return type.
function findUser(id: number): User | null {
  return db.find(u => u.id === id) ?? null;
}

never Return Type

A function that never returns (throws or loops forever) has the `never` return type.
function fail(msg: string): never {
  throw new Error(msg);
}

Typed Callback Parameters

When passing a function as an argument, TypeScript checks that the callback's signature matches the expected type.
function run(callback: (n: number) => void): void {
  callback(42);
}
run(n => console.log(n)); // OK

Overloaded Function Signatures

TypeScript allows multiple call signatures for a function. Define overloads before the implementation.
function pad(s: string, n: number): string;
function pad(s: string, c: string): string;
function pad(s: string, x: number | string): string {
  return typeof x === 'number' ? s.padStart(x) : s + x;
}

Quick Check

What return type should a function have if it only calls console.log and returns nothing?

Recap

Annotate parameter types and return types on functions to document the contract and catch bugs at call sites. Use void for no return, never for throwing functions, and union types for multiple return shapes.

Frequently asked questions

Is the “Parameter and Return Type Annotations” lesson free?

Yes — the full text of “Parameter and Return Type Annotations” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Parameter and Return Type Annotations”?

Add types to function inputs and outputs. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Parameter and Return Type Annotations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Parameter and Return Type Annotations
  2. Optional and Default Parameters
  3. Rest Parameters and Spread with Types
  4. void, never, and Function Signatures
← Back to TypeScript Academy