Logo

Home

About

Blog

Contact

Guestbook

Merch/Store

Portfolio

Privacy

TOS

Click to navigate

  1. Home
  2. Joshua R. Lehman's Blog
  3. Typing Functions: Parameters and Returns

Table of contents

  • Share on X
  • Discuss on X

Related Articles

Function Overloading in TypeScript
TypeScript
9m
Dec 28, 2025

Function Overloading in TypeScript

Function overloading allows you to define multiple function signatures for a single implementation in TypeScript. This guide covers how to write overloaded functions, the implementation signature, and practical use cases to improve type safety and clarity.

#TypeScript#Functions+3
Optional, Default, and Rest Parameters
TypeScript
8m
Dec 23, 2025

Optional, Default, and Rest Parameters

Functions often need flexibility in their parameters. TypeScript provides optional, default, and rest parameters to make function signatures adaptable while maintaining type safety. This guide covers how to use these features effectively.

#TypeScript#Functions+4
Arrow Functions and the 'this' Context
TypeScript
7m
Jan 2, 2026

Arrow Functions and the 'this' Context

Arrow functions in TypeScript and JavaScript have distinct behavior with the 'this' context compared to regular functions. This guide explains context binding, the differences between arrow and regular functions, and when to use each for optimal results.

#TypeScript#Functions+3

© Joshua R. Lehman

Full Stack Developer

Crafted with passion • Built with modern web technologies

2026 • All rights reserved

Contents

  • Why Type Function Parameters and Returns?
  • Basic Function Signatures
  • Typing Parameters
  • Rest Parameters
  • Optional and Default Parameters
  • Typing Return Values
  • Void Return Type
  • Implicit Return Types
  • Async Functions
  • Function Types and Type Aliases
  • Best Practices
  • Common Mistakes
  • Next Steps
TypeScript

Typing Functions: Parameters and Returns

December 18, 2025•3 min read
Joshua R. Lehman
Joshua R. Lehman
Author
Typing functions in TypeScript
Typing Functions: Parameters and Returns

Typing Functions: Parameters and Returns#

Functions are the heart of any program. Typing their parameters and return values in TypeScript helps catch bugs early, improves code readability, and makes your code easier to maintain.

Why type function parameters and returns? It ensures your functions are called correctly, helps IDEs provide better autocomplete, prevents runtime errors, and makes your code self-documenting.

Why Type Function Parameters and Returns?

Typing function parameters and return values:

  • Ensures functions are called with the correct arguments
  • Helps IDEs provide better autocomplete and documentation
  • Prevents runtime errors by catching type mismatches at compile time
  • Makes your code self-documenting and easier to understand

Basic Function Signatures

A function signature describes the types of its parameters and its return type.

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

Here, name is typed as string, and the function returns a string.

Typing Parameters

You can type each parameter explicitly:

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

Rest Parameters

Functions can accept a variable number of arguments using rest parameters:

function sum(...numbers: number[]): number {
  return numbers.reduce((total, n) => total + n, 0);
}
 
console.log(sum(1, 2, 3, 4)); // 10

Rest parameters must be the last parameter and are typed as an array.

Optional and Default Parameters

Optional Parameters

Parameters can be marked optional with ?:

function greet(name?: string): string {
  return `Hello, ${name ?? "Guest"}!`;
}
 
console.log(greet()); // Hello, Guest!
console.log(greet("Alice")); // Hello, Alice!

Optional parameters must come after required ones.

Default Parameters

You can provide default values:

function multiply(a: number, b: number = 1): number {
  return a * b;
}
 
console.log(multiply(5)); // 5
console.log(multiply(5, 2)); // 10

Default parameters are implicitly optional.

Typing Return Values

Always specify the return type explicitly for clarity:

function getLength(str: string): number {
  return str.length;
}

If you omit the return type, TypeScript infers it from the return statements.

Async Functions

Async functions always return a Promise of the return type:

async function fetchData(url: string): Promise<string> {
  const response = await fetch(url);
  return response.text();
}

Void Return Type

Functions that don't return a value have a void return type:

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

void means the function returns nothing.

Implicit Return Types

TypeScript can infer return types, so you can omit them:

function multiply(a: number, b: number) {
  return a * b; // inferred as number
}

However, explicitly typing return types is a good practice for public APIs.

Function Types and Type Aliases

You can define function types and use type aliases:

type BinaryOperation = (x: number, y: number) => number;
 
const subtract: BinaryOperation = (a, b) => a - b;

This helps reuse function signatures and improves readability.

Function Type Expressions

You can also declare variables with function types:

let greet: (name: string) => string;
 
greet = (name) => `Hello, ${name}!`;

Using Interfaces for Function Types

interface Comparator {
  (a: number, b: number): number;
}
 
const compare: Comparator = (x, y) => x - y;

Tip: Use type aliases or interfaces for complex function types to improve readability and reusability.

Best Practices

  • Always type function parameters and return values explicitly in public APIs
  • Use type aliases or interfaces for complex function types
  • Prefer explicit return types for clarity and maintainability
  • Avoid using any for parameters or return types
  • Use optional and default parameters to make functions flexible
  • Use rest parameters for variable argument lists
  • Document function behavior and parameter expectations

Common Mistakes

  • Forgetting to type parameters or return values, leading to implicit any
  • Using any instead of proper types, losing type safety
  • Mismatching parameter and argument types
  • Omitting return types in public functions, reducing clarity
  • Placing optional parameters before required ones (causes errors)
  • Forgetting that rest parameters must be last

Watch out: Optional parameters must come after required ones, and rest parameters must always be last in the parameter list.

Next Steps

Next, learn about optional, default, and rest parameters in detail to make your functions more flexible and expressive.


Questions about typing functions? Share your thoughts or examples in the comments!