Course contentsShow
← TypeScript
Lesson 246 of 1,7966. FunctionsFree lesson

noImplicitAny and Functions

Explore how the noImplicitAny compiler flag affects function parameter type inference.

noImplicitAny and Functions

You'll learn how the noImplicitAny compiler flag affects type inference in function parameters and when TypeScript will warn you about missing types.

What Is noImplicitAny?

The noImplicitAny flag is a compiler option that prevents TypeScript from silently inferring any when it can't determine a parameter's type. When enabled, TypeScript forces you to be explicit about types in situations where inference fails.

How It Affects Function Parameters

Without contextual typing (like when a function isn't a callback), TypeScript cannot infer parameter types. By default, it falls back to any:

// With noImplicitAny: false (default in some configs)
function greet(name) {  // 'name' implicitly has type 'any' — allowed
  return `Hello, ${name}`;
}

With noImplicitAny enabled, this same code produces an error:

// With noImplicitAny: true
function greet(name) {  // ERROR: Parameter 'name' implicitly has an 'any' type
  return `Hello, ${name}`;
}

// Fix by adding explicit annotation
function greet(name: string) {  // ✓ Works
  return `Hello, ${name}`;
}

When It Applies

The flag specifically targets implicit any types — situations where TypeScript would quietly give up on inference. It doesn't prevent you from explicitly writing any if needed, and it doesn't affect parameters where TypeScript can infer types through context (like callback parameters).

Key Takeaway: noImplicitAny forces you to explicitly type function parameters when TypeScript can't infer them, catching a major source of bugs by preventing silent type safety losses.