runtime.boot

Full Stack Systems
Production Mindset

CodeWithMihir

Engineering thoughtful products from interface to infrastructure.

CodeWithMihir

TypeScript Tutorial

TypeScript any Type Explained with Examples

Learn what the TypeScript any type does, why it is risky, when it appears, and safer alternatives for real projects.

Welcome back! I am Mihir, and in this lesson we will learn the any type in TypeScript.

any is the most flexible TypeScript type. It also removes most of the safety that TypeScript gives you.


What is any?

any means a value can be anything.

let data: any = "Mihir";

data = 25;
data = true;
data = { id: 1 };

TypeScript allows all of this because data is typed as any.


any Turns Off Type Checking

With any, TypeScript stops protecting you.

let value: any = "typescript";

value.toUpperCase();
value.toFixed(2);
value.notARealMethod();

TypeScript does not complain, even though some of this can crash at runtime.

That is why any should be used carefully.


Where any Commonly Appears

You may see any when:

  • migrating old JavaScript code
  • dealing with unknown third-party data
  • working with libraries that have weak types
  • temporarily skipping a difficult type
  • writing quick experiments

Example:

function logData(data: any): void {
  console.log(data);
}

This accepts anything, but it gives up type safety.


any in Arrays

let items: any[] = [1, "two", true];

items.push({ name: "Mihir" });
items.push(null);

This is allowed, but it becomes hard to know what each item is.

Prefer a real union type when possible:

let items: Array<string | number | boolean> = [1, "two", true];

Now TypeScript still understands the allowed values.


any in Functions

function getValue(input: any): any {
  return input;
}

This function accepts anything and returns anything.

That means TypeScript cannot help much:

const result = getValue("hello");

result.toFixed(2);

TypeScript allows it because result is any, but a string does not safely support toFixed().


Better Alternative: unknown

Use unknown when you truly do not know the type yet.

let value: unknown = "TypeScript";

if (typeof value === "string") {
  console.log(value.toUpperCase());
}

unknown forces you to check the type before using it.

This is safer than any.


Better Alternative: Specific Types

Instead of:

function printUser(user: any): void {
  console.log(user.name);
}

Use:

type User = {
  name: string;
  email: string;
};

function printUser(user: User): void {
  console.log(user.name);
}

Now TypeScript knows exactly what user should look like.


When any Is Acceptable

any is not evil, but it should be temporary or isolated.

It can be acceptable when:

  • you are migrating a project step by step
  • a library has no usable types
  • you are intentionally creating a very flexible escape hatch
  • you add a comment explaining why it is needed

Even then, try to keep any close to the boundary and convert it into real types quickly.


Common Mistake: Using any to Silence Errors

const user: any = getUser();

console.log(user.profile.address.city);

This may hide a real bug.

Better:

type User = {
  profile?: {
    address?: {
      city?: string;
    };
  };
};

const user: User = getUser();

console.log(user.profile?.address?.city);

Now missing data is visible in the type.


Quick Reference Summary

ConceptExample
Any variablelet data: any
Any arraylet items: any[]
Any parameterfunction log(data: any)
RiskTypeScript stops checking safely
Better unknownlet data: unknown
Better specific typetype User = { name: string }

Practice

Replace any with safer types:

function printProduct(product: any): void {
  console.log(product.title);
  console.log(product.price);
}

Better:

type Product = {
  title: string;
  price: number;
};

function printProduct(product: Product): void {
  console.log(product.title);
  console.log(product.price);
}

What You've Learned

You now understand:

  • What any means
  • How any disables type checking
  • Where any commonly appears
  • Why any can cause runtime bugs
  • Why unknown is safer
  • Why specific types are better
  • When any may be acceptable temporarily

What's Next?

In the next lesson, we will learn the unknown Type in TypeScript.

We will see how it lets you accept uncertain data without giving up safety.


Need Help?

  • Have questions, confusion, or want to know more? Contact me

Use any like an emergency exit: helpful sometimes, but not where you want to live.