# any vs unknown

> The dangerous escape hatch vs the safe mystery box.

Subject: TypeScript | Lesson 2 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/any-vs-unknown
Previous: [Core Primitives](https://codewithanimation.in/typescript/primitive-types.md) | Next: [The never Type](https://codewithanimation.in/typescript/never-type.md)

## The Escape Hatch and the Mystery Box

Sometimes you don't know what type a value will be—perhaps it's coming from a third-party API or user input. TypeScript gives you two options: `any` and `unknown`.

`any` is an escape hatch. It completely disables the type checker for that variable. You can do literally anything with it, just like plain JavaScript.

`unknown` is a mystery box. It tells the compiler "I don't know what this is yet." Unlike `any`, TypeScript will block you from doing anything with an `unknown` value until you perform a check to prove what it actually is.

## Why prefer unknown over any?

Using `any` brings back all the runtime crashes that TypeScript was designed to prevent. If you type a variable as `any`, TypeScript will let you call `.toUpperCase()` on it, even if it turns out to be a number at runtime. The app will crash.

Using `unknown` forces you to be safe. TypeScript will throw a red squiggle if you try to call `.toUpperCase()` on an `unknown` value. You must first write an `if (typeof val === 'string')` check before the compiler allows the method call.

## Narrowing the unknown

To use an `unknown` value safely, you must 'narrow' its type. This is typically done using the `typeof` operator or type predicates.

```typescript
let input: unknown = getNetworkData();

// Error: Object is of type 'unknown'.
input.toUpperCase();

if (typeof input === 'string') {
  // Safe! TypeScript now knows input is a string here.
  input.toUpperCase();
}
```

## Common mistake: The lazy 'any' trap

When faced with a complex type error they don't understand, developers often resort to typing the variable as `any` just to make the red squiggly go away.

This is a fatal trap. Every `any` you add acts like a virus, infecting your codebase and quietly disabling type safety wherever that variable flows.

## Tip: Ban 'any' globally

Most modern production codebases configure their `tsconfig.json` with `strict: true` (which implies `noImplicitAny`) and use ESLint's `@typescript-eslint/no-explicit-any` rule to completely ban its use.

## Try it: Try it

Toggle between `any` and `unknown` to see how the compiler reacts to a risky method call.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/any-vs-unknown

## Check yourself

### Question 1

What happens when you try to access a property on an 'unknown' variable without checking its type first?

- A. The code runs fine, but might crash at runtime.
- B. TypeScript throws a compile-time error.
- C. TypeScript automatically coerces it to a string.

Correct answer: B

TypeScript blocks any operation on an 'unknown' variable until you narrow its type (e.g., using an 'if' statement), guaranteeing runtime safety.

- Why not A: That is the behavior of 'any', not 'unknown'.
- Why not C: TypeScript does not coerce types; it strictly enforces them.

## Remember this

- `any` disables the type checker. It is dangerous and should be avoided.
- `unknown` represents a value whose type is not yet known. It is safe.
- You must 'narrow' an `unknown` value (prove its type) before TypeScript will let you use it.
