Course contentsShow
← TypeScript
Lesson 127 of 1,7963. Basic TypesFree lesson

When any Appears by Default

Learn situations where TypeScript infers any implicitly and how noImplicitAny prevents this.

When any Appears by Default

What you'll learn: You'll discover the situations where TypeScript automatically infers any and how to prevent this from happening.

The Implicit any Problem

TypeScript tries to infer types everywhere, but sometimes it simply doesn't have enough information. When TypeScript can't figure out what type something should be, it falls back to any by default—silently removing all type safety from that value.

Think of it like a teacher who, when they can't read your handwriting, just marks everything as "correct" instead of asking you to clarify. That's convenient in the moment but defeats the purpose of checking your work.

Common Scenarios Where any Appears

Function parameters without annotations:

function greet(name) {  // name is implicitly 'any'
  return "Hello, " + name;
}

TypeScript doesn't know what name should be, so it defaults to any. Now you can pass numbers, objects, anything—and lose all type safety.

Variables without initialization or annotation:

let data;  // implicitly 'any'
data = 42;
data = "text";  // No error!

The noImplicitAny Safety Net

The noImplicitAny compiler option (included in strict mode) turns these silent failures into errors:

// With noImplicitAny enabled:
function greet(name) {  // ❌ Error: Parameter 'name' implicitly has an 'any' type
  return "Hello, " + name;
}

Now TypeScript forces you to be explicit: either add a type annotation or let TypeScript infer it from context.

Key Takeaway: TypeScript defaults to any when it lacks type information, silently disabling safety. Enable noImplicitAny to catch these situations and maintain type safety throughout your code.