Skip to content
Architecture & TypesChapter 15 / 57Intermediate6 min read

Strict Typing

TypeScript strict mode and the no any rule. Avoid unchecked casts and parse API input at the boundary, with real examples of the bugs this prevents.

On this page

1. What is this practice?

TypeScript only helps when you tell the compiler the truth. The moment you write any, the compiler stops checking that value, and writing as User does not turn the data into a User, it only silences the compiler.

Strict typing means: anything outside your control (an API response, a form, JSON.parse, env) is checked first, and then all the code inside runs on typed values you can trust.

2. Why does it matter?

A real bug often looks like this: the API sent price as a string ("500"), the code wrote as Product, and the cart total became 0 + "500", which is "0500". The compiler said nothing, because it was told the wrong thing.

With any and castsWith strict typing
A wrong shape crashes far away at runtimeA wrong shape is caught right at the boundary
any spreads from one file to manyThe compiler knows the type of every value
Renaming a field breaks nothing, the bug reaches productionRenaming a field fails the build, you fix it everywhere
Refactoring is scaryThe compiler tells you what needs to change
Editor autocomplete does not workAutocomplete and jump to definition work properly

3. When to follow it

LevelHow importantWhy
Learning projectstrict: true from day one, build the habit of avoiding anyA bad habit is hard to drop later. Understanding the errors is learning TypeScript
Production codeA must. tsc --noEmit in CI and the no-explicit-any lint ruleA single any ends the guarantee for the whole data flow
Senior levelDesign contracts with types, and set the boundary parsing pattern for the teamA senior owns the type safety of the whole codebase, not just their own code

4. Bad vs good code

Bad: any and a cast

ts
type Product = {
  id: string;
  price: number;
};

export async function getProduct(id: string) {
  const response = await fetch(`/api/products/${id}`);
  const data: any = await response.json();

  return data as Product;
}

const product = await getProduct("p1");
const total = 0 + product.price;

Here data can be anything. as Product only silences the compiler. If the server sent price: "500", total becomes a string, and the bug shows up on the cart page, not where the fetch happens.

Good: parse at the boundary

ts
import { z } from "zod";

const productSchema = z.object({
  id: z.string(),
  price: z.number(),
});

type Product = z.infer<typeof productSchema>;

export async function getProduct(id: string): Promise<Product> {
  const response = await fetch(`/api/products/${id}`);

  return productSchema.parse(await response.json());
}

Now if a wrong shape arrives, parse throws an error right where the data came in. Every function inside can trust Product.

What is the difference?

ThingBad codeGood code
Response typeany, then as Productunknown parsed into Product
Wrong dataCrashes somewhere else, laterAn error at the boundary right away
Source of the typeWritten by hand, separate from the schemaz.infer from the schema, one source
CompilerSilencedActually checking

5. How to apply it

Steps to apply strict typing
strict true in tsconfig
no-explicit-any as an error in ESLint
At every boundary, parse unknown with a schema
Inner functions accept only typed values
Run tsc --noEmit in CI
json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true
  }
}

The boundaries are: a fetch response, form data, URL params, JSON.parse, localStorage, env variables and third-party webhooks. Treat all of these as unknown until they are parsed.

In catch (error), error is unknown too. Narrow it with if (error instanceof Error), not with any.

6. Exceptions and common mistakes

Some things look like casts but are fine: as const (makes a value a literal, it does not lie) and satisfies (checks the shape without changing the type).

Many teams allow any or @ts-expect-error in test files, or turn on strict mode later in a prototype. It feels fast at first, but every shortcut has to be hunted down later. The rule in this course is strict: no shortcuts in production code.

If a library has bad types, make a typed wrapper function. Keep the cast in that one place, not across the whole app.

Mistake: Writing as just to make an error go away

Why? A cast checks nothing at runtime, it only closes the compiler's eyes. The bug stays, it just hides.

Better: Narrow the value with a schema or a type guard so the compiler truly knows the shape.

Mistake: Escaping with a non-null assertion, like user!.name

Why? If user really is undefined, it crashes and the error message tells you nothing.

Better: Write a clear null check, and throw a descriptive error or return early when the value is missing.

Mistake: Using the JSON.parse result directly

Why? JSON.parse returns any, so without a check that any spreads through the whole code.

Better: Treat the result as unknown and pass it through a schema parse.

Mistake: Leaving ts-ignore or ts-expect-error behind

Why? Today's error is hidden, and tomorrow's new error will hide on the same line too.

Better: Fix the real type problem. If it is a library bug, contain it in a wrapper and revisit it on upgrade.

7. What to learn before and after

Suggested order
Fail Fast
Strict Typing
Input Validation at the Boundary
Generated Database Types

Final quick revision

CheckYes / No
strict: true in tsconfigIt should be there
any in the codeNo
An as cast that hides an errorNo
@ts-ignore / @ts-expect-errorNo
Outside data is parsed before it comes inYes
Type and schema from one sourceYes, z.infer