Logo

Home

About

Blog

Contact

Guestbook

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

Introduction to TypeScript: Why Static Typing Matters
TypeScript
7m
Nov 6, 2025

Introduction to TypeScript: Why Static Typing Matters

TypeScript has evolved from a Microsoft experiment to the industry standard for building scalable JavaScript applications. Understanding why static typing matters is the first step in mastering modern web development. This comprehensive introduction explores the benefits, use cases, and real-world impact of adopting TypeScript.

#TypeScript#JavaScript+6
Type Assertions and Type Casting
TypeScript
6m
Dec 13, 2025

Type Assertions and Type Casting

Type assertions tell TypeScript to trust your judgment about a value's type. While they're powerful tools for working with DOM elements, API responses, and type narrowing, they can also bypass type safety if misused. This guide covers the as syntax, angle-bracket syntax, when to use assertions safely, and the crucial difference between TypeScript assertions and runtime type casting. Learn to wield this tool responsibly while maintaining type safety in your applications.

#TypeScript#Type Assertions+4
The any, unknown, and never Types
TypeScript
8m
Dec 8, 2025

The any, unknown, and never Types

TypeScript has three special types that handle edge cases: any (the escape hatch), unknown (the type-safe alternative), and never (for impossible values). Understanding when and how to use these types is crucial for writing safe yet flexible TypeScript code. This guide covers the differences between these types, shows you how to work with unknown safely, and demonstrates how never enables exhaustive type checking and prevents runtime errors. Learn to use these types effectively while maintaining type safety.

#TypeScript#Type System+5

© Joshua R. Lehman

Full Stack Developer

Crafted with passion • Built with modern web technologies

2025 • 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!