C.W.K.
Stream
Lesson 02 of 05 · published

`noImplicitAny`

~6 min · strict-mode, no-implicit-any

Level 0Curious
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete
"If the compiler can't tell what type something is, it shouldn't default to 'anything goes.'"

What it catches

Without noImplicitAny, the compiler silently types un-annotated, non-inferable values as any. A function parameter with no annotation and no callsite to infer from? any. A variable declared without initialization or annotation? any. The 'don't check me' type spreads through your codebase without warning.

With the flag on, every such case becomes a compile error. You must either annotate or rewrite the code so inference can succeed. Explicit any (: any) is still allowed — the flag only catches the implicit case.

Why it's the most important strict flag

Of the seven strict-family flags, noImplicitAny catches the broadest category of latent unsafety. It's the foundation; the others build on it. Turn this on first when migrating an unstrict codebase to strict.

The migration playbook

On a legacy codebase, turning on noImplicitAny usually produces hundreds of errors. The migration path:

  1. Annotate hot-path public APIs first.
  2. Sweep through file-by-file from leaves inward.
  3. Use the IDE's quick-fix to insert any explicitly for the truly-untyped cases. This makes the implicit anys visible without breaking anything.
  4. Replace each explicit any with a real type over time.
noImplicitAny is the line between 'type system catches my bugs' and 'type system shrugs and lets things through.' Always on.

Code

Implicit vs explicit any·typescript
// Without noImplicitAny — silently typed `any`.
function loose(x) {
  return x.toUpperCase();    // ✅ compiles — x is implicit any
}
loose(42);                    // crashes at runtime

// With noImplicitAny:
function strict(x) {          // ❌ Parameter 'x' implicitly has 'any' type
  return x.toUpperCase();
}

// Fix 1: annotate.
function strictFix(x: string): string {
  return x.toUpperCase();
}

// Fix 2: explicit any (when you genuinely don't know yet).
function quickFix(x: any) {
  // any used explicitly — at least the reader sees it
  return x.toUpperCase();
}

External links

Exercise

Write a function that takes a parameter without annotating it. Turn noImplicitAny off, then on. Note the compile error appears only with the flag on. Now annotate the parameter — error disappears. Now write a callsite that passes the wrong type — caught by the type system.
Hint
The flag is binary. With it off, you have JavaScript with optional types. With it on, you have TypeScript. Most projects forget how much the flag is doing until they try a codebase without it.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.