Direct answer
In this monorepo, tsconfig has strict, strictTemplates, and strictInjectionParameters all set to true, yet 54 explicit ': any' uses and 46 'as any' casts survive across 3884 TypeScript files. Strict mode eliminates accidental type errors, not deliberate ones: any stays an escape hatch always available to whoever is in a hurry, and no compiler option can close it entirely.
"I have strict: true, so my codebase is type-safe" is a
sentence I hear often and rarely see actually verified on a real
project, at real size. Strict mode is necessary. It is not
sufficient. The gap between those two adjectives is worth 100
explicit type breaches on this same portfolio, all of them past code
review, all of them compiling without a single error.
Method disclaimer: the numbers come from a direct grep on
the source (grep -ro ": any\b" src/app and
grep -ro "as any\b" src/app), not from a theoretical
analysis. They are verifiable by anyone with access to the
repository.
What strict mode actually does (and does not do)
TypeScript's strict: true flag is actually a bundle of
eight distinct flags (strictNullChecks,
noImplicitAny, strictFunctionTypes, and
others). This project has all of them on, plus two Angular-specific
extensions: strictTemplates (type-checking inside HTML
templates) and strictInjectionParameters (typed
dependency injection).
What strict mode really eliminates are accidental errors: a
variable the compiler would have inferred as any
without noImplicitAny, an access to a potentially
null property without strictNullChecks.
These are errors nobody writes on purpose: they emerge from an
absence of constraints, not from an explicit developer decision.
What strict mode does NOT eliminate is the deliberate escape hatch:
an any written by hand, or an as SomeType
cast that tells the compiler "trust me" without it being true. No
compiler flag can stop a developer under pressure from writing
const data: any = response.body to make the code
compile and move on.
The datum: 100 explicit breaches across 3884 files
The exact count, as of today:
- 54 occurrences of
: any(explicit any typing). - 46 occurrences of
as any(forced cast). - 37 generic functions (
function name<T>) as a comparison point: the "advanced" TypeScript this article does not cover, coexisting with those same 100 breaches. - 3884 total TypeScript files in
src/app.
100 breaches across 3884 files is under 3% of the files, and many
occurrences share the same file. This is not a red alarm. It is
empirical proof that even in a disciplined codebase with every
available strictness flag turned on, any never
disappears entirely: it shrinks to a measurable residue, not to
zero.
Where the anys actually end up: system boundaries
Not all anys are equal. Looking at where they cluster
in the code, three recurring patterns emerge:
-
Boundaries with untyped or partially typed external
libraries. A third-party library without up-to-date
@types, or with generic types too broad to be useful, pushes towardas anyas the faster path compared to writing a custom type declaration for code you do not control. -
Ambiguous browser/SSR APIs. Code that has to
work both server-side (Node/Express) and client-side (browser),
where a global object has a different shape in the two
environments, sometimes ends up using
anyinstead of an explicit union type, especially when theisPlatformBrowsercheck already handles the logic and the type feels redundant. -
Deadline pressure. The most honest and most
common case: a component that has to ship, a complex type to
derive correctly (nested generics, mapped types), and the
pragmatic choice to unblock the commit with
anyinstead of spending twenty minutes writing the correct type.
The trade-off: unknown, type guard, or honest any
This is where the article gets controversial. The textbook answer
is always "use unknown instead of any,
then narrow with a type guard". It is correct in theory. In the
practice of a boundary with an external library,
unknown shifts the cost of unblocking from the entry
point to every single downstream use site, because every property
access first requires an explicit narrowing step.
Sometimes that cost is worth it (data crossing a security boundary,
user input, untrusted API responses). Sometimes it is not: an
any declared with a comment explaining why
(// any: library X has no types, manually verified v2.1)
is more honest than an as SomeType cast that pretends
a type guarantee the compiler is not actually verifying. A lying
cast is worse than a declared any, because the first
gives the next developer reading that code a false sense of safety.
Eliminating the last remaining anys has diminishing
marginal returns: the first hundred are probably worth the effort,
the last ten, often tied to genuinely untypeable external
boundaries, are not.
What I would do differently
I would not change the choice of 100% strict mode: it remains the
correct baseline. What I would introduce is a dedicated lint rule
(@typescript-eslint/no-explicit-any in "warn" mode, not
"error") with a mandatory comment on every occurrence explaining
the reason for the boundary, instead of letting any
pass silently through code review just because it compiles. The
problem is not that 100 anys exist: it is that none of
the 100 declares why it is there.
Frequently asked questions
What does TypeScript's strict flag actually turn on?
strict: true turns on a bundle of eight distinct flags, including strictNullChecks, noImplicitAny, and strictFunctionTypes, which together eliminate accidental type errors (variables inferred as any, accesses to potentially null properties) but do not prevent the deliberate use of any explicitly written by the developer.
Why does a project with 100% strict mode still have any usage?
Because any remains an escape hatch always available to the compiler: no strictness flag can stop a developer from explicitly writing any to make code compile quickly, especially at boundaries with untyped external libraries or under deadline pressure.
Is unknown better than any when a type is unclear?
It depends on context: unknown is safer because it requires explicit narrowing before every use, but it shifts the cost from the entry point to every downstream use site. On data crossing a security boundary it is worth it, on genuinely untypeable boundaries a declared any with an explanatory comment is often more honest.
Why can an 'as SomeType' cast be worse than a declared any?
Because a forced cast pretends a type guarantee the compiler is not actually verifying, giving the next developer reading that code a false sense of safety, while an explicitly declared any is at least honest about the fact that the value is unverified.
Is it worth eliminating every remaining any in a strict codebase?
Not all of them: eliminating any usage has diminishing marginal returns. The first ones are often easy to fix, but the last remaining ones tend to be tied to genuinely untypeable external boundaries, where the cost of removing them exceeds the real benefit in type safety.
Commenti
Caricamento commenti...
Sign in to comment