Course contentsShow
← TypeScript
Lesson 156 of 1,7964. Type InferenceFree lesson

Annotating Function Parameters

When and why function parameters almost always need explicit type annotations to avoid implicit any.

What You'll Learn

You'll learn why function parameters almost always require explicit type annotations and what happens when you skip them.


The Core Problem: TypeScript Can't Read Your Mind

When you write a variable like let count = 5, TypeScript sees the 5 and infers the type number. But when you write a function parameter like function greet(name) { ... }, TypeScript has no value to look at—just an empty slot waiting for data.

Without a value, there's nothing to infer from.

By default, TypeScript will assign the any type to unannotated parameters, which completely disables type checking for that value. This is the opposite of safe code—it's like opening a door for bugs to walk through.


Why Parameters Are Different

Think of it this way: a variable gets its type from what you assign to it. But a parameter gets its type from what might be passed to it—which could be anything! TypeScript needs you to specify what you expect.

function double(value) {  // ❌ Implicit 'any' type
  return value * 2;       // No safety—what if value is a string?
}

function double(value: number) {  // ✅ Explicit annotation
  return value * 2;               // TypeScript knows it's safe
}

The noImplicitAny Safety Net

This is why the noImplicitAny compiler option exists. When enabled (which strict mode does automatically), TypeScript will error on unannotated parameters, forcing you to be explicit.

Without it, you'd silently get any types everywhere, defeating the purpose of TypeScript.


Key Takeaway: Function parameters almost always need explicit type annotations because TypeScript has no value to infer from—you must tell it what types you expect to receive.