How to Type Function Parameters in TypeScript

What you’ll build or solve

You’ll define functions with clearly typed parameters, including primitives, objects, and unions.

When this approach works best

Typing function parameters works best when you:

  • Build reusable utilities that other parts of your app depend on
  • Validate API input before processing it
  • Migrate JavaScript functions to TypeScript

It also helps when collaborating with others who rely on clear function contracts.

This is a bad idea only if you leave parameters untyped and rely on implicit any. That weakens type safety.

Prerequisites

  • TypeScript installed
  • A .ts file
  • Basic knowledge of functions and types

Step-by-step instructions

Step 1: Add type annotations to parameters

Use the syntax parameterName: Type.

functiongreet(name:string) {
return`Hello,${name}`;
}

TypeScript now enforces correct usage:

greet("Alex");// OK
greet(42);// Error

For multiple parameters, type each one:

functionadd(a:number,b:number) {
returna+b;
}

Each parameter must include its own type annotation.

Step 2: Type object parameters

You can type object parameters inline or with an interface.

Option A: Inline object type

functioncreateUser(user: { id:number; name:string }) {
returnuser.name;
}

Option B: Using an interface

interfaceUser {
  id:number;
  name:string;
}

functioncreateUser(user:User) {
returnuser.name;
}

Use interfaces when the object shape is reused or grows over time.

Step 3: Use union types for flexible input

If a parameter can accept more than one type, use a union.

functionformatId(id:string|number) {
return`ID:${id}`;
}

TypeScript allows either type:

formatId(101);
formatId("A102");

Union types are helpful when inputs vary but remain predictable.

What to look for

  • Always use parameterName: Type
  • Each parameter must be typed individually
  • Optional parameters use ?, for example title?: string, and must come after required ones
  • Default values can be added, for example factor: number = 2, and the type is inferred from the default
  • Rest parameters use typed arrays, for example ...numbers: number[]
  • Enable noImplicitAny in tsconfig.json to prevent untyped parameters
  • Use interfaces or type aliases for complex object parameters

Examples you can copy

Example 1: Login function

interfaceCredentials {
  username:string;
  password:string;
}

functionlogin(credentials:Credentials) {
return`Logging in${credentials.username}`;
}

This prevents missing or incorrectly shaped input.

Example 2: Flexible search function

functionsearch(query:string,limit:number) {
return`Searching for${query}, limit${limit}`;
}

Both parameters are explicitly typed.

Example 3: Controlled log levels

functionlog(level:"info"|"warning"|"error",message:string) {
return`[${level}]${message}`;
}

The first parameter only accepts specific string values.

Common mistakes and how to fix them

Mistake 1: Forgetting to type parameters

You might write:

functionadd(a,b) {
returna+b;
}

Why it breaks: In strict mode, parameters may become any, which removes type safety.

Correct approach:

functionadd(a:number,b:number) {
returna+b;
}

Always add explicit types in TypeScript.

Mistake 2: Mixing optional and required parameters incorrectly

You might write:

functiongreet(title?:string,name:string) {
return`${title}${name}`;
}

Why it breaks: Required parameters cannot follow optional ones.

Correct approach:

functiongreet(name:string,title?:string) {
return`${title??""}${name}`;
}

Place optional parameters after required ones.

Troubleshooting

  • If you see “Parameter implicitly has an any type,” enable strict mode and add explicit annotations.
  • If a function call fails type checking, verify that the argument types match the parameter types.
  • If unions cause errors inside the function, narrow the type before using type-specific methods.
  • If autocomplete does not suggest properties, confirm the parameter is properly typed.

Quick recap

  • Add : Type after each parameter name
  • Type object parameters inline or with interfaces
  • Use unions for flexible input
  • Keep optional parameters after required ones
  • Enable strict settings to avoid implicit any