TypeScript any: When to Use It and How to Keep It in Check
- Average speed
- km/h
- Result
- Total time
- 30 min
- You entered
- Distance
- 5 km
- You entered
TypeScript’s any type disables type checking for a value, and you should use it only as a last resort when you genuinely cannot know the shape of data at compile time. To keep it in check, you replace any with unknown, narrow types with guards, and use ESLint rules to ban or flag its usage.
Formula
The calculator works out your average pace and speed from a run or ride. You enter the total time in minutes and the distance in kilometers. The pace is the time divided by the distance, and the speed is the distance divided by the time in hours.
Pace (min/km) = t / d
Speed (km/h) = d / (t / 60)
Where:
- t = total time in minutes
- d = distance in kilometers
Worked Example
Using the defaults: t = 30 minutes, d = 5 km.
Pace = 30 / 5 = 6 min/km
Speed = 5 / (30 / 60) = 5 / 0.5 = 10 km/h
So the calculator shows 6.00 min/km and 10.00 km/h.
What Is TypeScript’s any Type?
In TypeScript, every variable has a type. The any type is the escape hatch: it tells the compiler to skip type checking for that value entirely. You can assign anything to it, call any method on it, and pass it anywhere. It behaves like plain JavaScript, which is both its strength and its danger.
TypeScript was created to add static types to JavaScript, and any is the one type that completely opts out of that safety. When you use it, you lose autocompletion, refactoring support, and the compiler’s ability to catch mistakes before runtime.
When Should You Actually Use any?
There are a few legitimate situations where any is the pragmatic choice:
- Migrating a JavaScript codebase – When you convert a large JS project to TypeScript, you might temporarily use
anyto get the code compiling, then gradually add real types. - Third-party libraries without types – If a library has no type definitions and you don’t want to write your own, you can use
anyas a stopgap. - Very dynamic data – Data from a JSON API that changes shape, or values from a web worker, might be impossible to type ahead of time.
- Prototyping – When you’re sketching out an idea and don’t want to think about types yet.
In all these cases, the goal is to remove any later. It’s a debt you’re consciously taking on, not a permanent solution.
Why any Is Dangerous
The whole point of TypeScript is to catch errors before they reach users. any turns off that safety net. A typo like user.nmae won’t be flagged, and you’ll only discover the problem when the code runs and throws an error.
It also spreads. If a function returns any, every variable that receives that result becomes any too, and the lack of checking propagates through your codebase. This is often called the “any leak” – a single untyped value can contaminate a whole module.
How to Keep any in Check
You don’t have to ban any entirely, but you should control it. Here are the practical steps:
- Prefer
unknowninstead. Theunknowntype is the type-safe version ofany. You can’t use anunknownvalue until you narrow it with a type guard, liketypeofor a custom check. This forces you to handle the data deliberately. - Use ESLint with typescript-eslint. The rule
@typescript-eslint/no-explicit-anywarns whenever you writeany. You can set it to warn or error, and add an allowlist for specific files (like migration files). - Add a lint rule for implicit any. The rule
no-implicit-anycatches cases where TypeScript infersanybecause it can’t figure out the type, like a callback parameter with no annotation. - Create a type for the data. If you’re using
anybecause you don’t know the shape, define an interface or type alias. Even a rough type with optional fields is better thanany. - Use
unknownin generic positions. For example,function identity<T>(value: T): Tis better thanfunction identity(value: any): any.
Common Mistakes with any
One frequent mistake is using any for function parameters because you don’t want to write a type. Instead, use a union type or a generic. Another is casting with as any to silence a type error – that hides the problem rather than fixing it.
People also confuse any with unknown. Remember: any disables checking, unknown requires you to prove what the value is before using it. If you have a value from an external source, unknown is almost always the better choice.
Alternatives to any
Depending on the situation, you might use:
unknown– when you have a value that could be anything but you’ll narrow it later.- Generics – when you want a function to work with any type but still keep type safety (e.g.,
function first<T>(arr: T[]): T | undefined). - Union types – when the value can be one of a few known types, like
string | number. - Interfaces or type aliases – when you know the shape, even partially.
Record<string, unknown>– for objects with arbitrary keys but unknown values.
Frequently Asked Questions
Is it okay to use any in a small project?
It’s tempting, but even in a small project, any can hide bugs and make the code harder to refactor. If the project is small, it’s easier to type things properly from the start. Use unknown or a simple interface instead.
What’s the difference between any and unknown?
any disables all type checking – you can do anything with it. unknown is the safe counterpart: you can’t use it until you narrow it with a type guard. Think of unknown as “I don’t know what this is yet,” and any as “I don’t care what this is.”
How do I ban any in my codebase?
Use ESLint with the typescript-eslint plugin. Add the rule @typescript-eslint/no-explicit-any set to "error". You can also enable @typescript-eslint/no-unsafe-assignment and related rules to catch any leaking into your code.
Can I use any for JSON data from an API?
You could, but it’s better to define an interface for the expected response. If the API is truly unpredictable, use unknown and then validate the data at runtime with a library like zod or a custom type guard. That way you keep type safety.
Does using any affect performance?
No, TypeScript types are erased at compile time, so any has zero runtime cost. The downside is purely in development: you lose autocompletion and the compiler’s error checking, which can lead to more bugs that cost time later.
Is it okay to use any in a migration?
Yes, many teams do this when converting a large JavaScript codebase. Just make sure you track it – use a lint rule to flag new any occurrences, and gradually replace existing ones. It’s a temporary tool, not a permanent solution.
Related
Formula used
in t | Total time | min | 30 | 0 in d | Distance | km | 5 | 0 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.