Interview: JSON.parse leaked any everywhere
A runtime crash the types said was impossible. Find the any, then validate at the boundary. A free TypeScript lesson with examples.
More on TypeScript interviews
- TypeScript Interview: What Interviewers Actually Test
- Type vs Interface: TypeScript Interview Question
- Implement Pick, Omit and DeepReadonly: TypeScript Interview
- Interview: the new status nobody handled
- Interview: type a fetch wrapper
- Enum or Union of Strings? TypeScript Interview Question
- 25 TypeScript Interview Questions with Answers
Keep learning
Overview
chapters · typescript interviews · ticket 1 “JSON.parse leaked any everywhere” A runtime crash the types said was impossible. Find the any (the type TypeScript never checks), then validate at the boundary: the one place where outside data enters your code. any is contagious type guards schema validation Checkout crashes on reduce of undefined. tsc reports zero errors. Treat outside data as unknown and validate it once, at the boundary.
Checkout page white-screens for some users
TypeError: Cannot read properties of undefined (reading 'reduce'). Started after the orders service deploy this morning. CI is green. tsc --strict passes. tsc errors checkout errors backend change items → lineItems
checkout.ts · compiles cleanly
Order (unchecked) Why no compile error? res.json() is typed Promise<any>, and any fits into every type, including Order. So TypeScript accepts it without looking inside.
From a green build to a crash
res.json() returns any loadOrder(): Promise<Order> (unchecked) TypeScript now trusts that items exists the server actually sent lineItems runtime: order.items === undefined calling reduce on undefined throws TypeError: Cannot read properties of undefined (reading 'reduce')
Where does any enter, and how far does it spread?
Bad order payload Names the boundary and the shape, so the AI cannot guess. Bans the three escape hatches (as, !, : any) that switch checks off. schema / guard trusted from here Parse, don't trust: one checkpoint where outside data becomes typed data.
No AI from here
any versus unknown, in one breath? Anything is assignable to both (any value fits). That makes both "top types". top type 'u' is of type 'unknown'. Why does strict mode not catch this? noImplicitAny only flags any that TS had to invent. implicit vs explicit any typed lint rules Annotation, as, or satisfies on JSON.parse? Only runtime code checks runtime data. types are erased Compile-time tools : T, as T, satisfies T. Emit no JS. Runtime tools typeof, in, Array.isArray, a schema. Run on real data. Where else does any sneak in? JSON.parse, res.json(), untyped libraries, and any in .d.ts files (type-only declaration files). Array.isArray on unknown narrows to any[]. Catch clause variables are unknown under strict (useUnknownInCatchVariables). Find them: lint rules, or a type-coverage report in CI (a tool that counts how many expressions are any). knows the usual sources measures it Hand-written guard or schema library? Schema when the type is derived from it: one source of truth. A hand guard is fine for one small shape, but it can drift from the type. Either way, validate once at the edge, not in every component. single source of truth validate at the edge
The lead asks: “Do we validate every response?”
What do you propose? Schema-validate every API response Shape changes fail loudly at the edge with a clear message. Small CPU cost per response. Runtime cost Generate types from OpenAPI (a file describing the API), no runtime check Types match the spec, not the deploy. A server that drifts from its spec still crashes you. Keep casting with as Zero effort, zero safety. The next rename crashes prod again. Validate in dev only, cast in prod Catches drift in testing, saves CPU in prod. A prod-only payload still slips through.
Did you show these?
Said what TS believed order was Found the any at res.json() Explained why tsc stayed green Rejected as Order as a fix Validated once, at the boundary Derived the type from the validator Named a lint rule to stop the next one
Remember
any is contagious: everything it touches turns any. Annotations and as on any check nothing. Outside data is unknown until a runtime check says otherwise. ! Cheat code → Go deeper: any, unknown and never, type predicates, satisfies and derived types, type a fetch wrapper.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.