Bits Kingdom logo with a hexagon lattice, uppercase text in white, and a minimalistic design.

Strictly Typed: Navigating Common TypeScript Pitfalls and FAQ

TypeScript From Zero to Production-Ready Code - Chapter 9

Home / Development / Strictly Typed: Navigating Common TypeScript Pitfalls and FAQ

No matter how experienced you are with TypeScript, you’ll eventually encounter confusing type errors, tricky edge cases, or design dilemmas. Over time, I’ve run into (and solved) most of these headaches in production code. This chapter is your reference guide for avoiding common anti-patterns, troubleshooting recurring issues, and getting quick answers to frequent questions.

3D TypeScript logo for a series of posts.

1. Pitfall: Misusing any in TypeScript

The Problem: Using any disables type safety entirely.

let data: any = fetchSomething();
data.toUpperCase(); // runtime error if data is a number

The Fix: Prefer unknown when the type is unclear—it forces narrowing.

function handle(input: unknown) {
  if (typeof input === "string") {
    return input.toUpperCase();
  }
}

2. Pitfall: Object is Possibly null

The Problem: Accessing DOM elements, context values, or optional props without checking for null.

document.getElementById("header").innerHTML = "Hello"; // ❌

The Fix: Use optional chaining or conditional logic.

document.getElementById("header")?.innerHTML = "Hello";

3. Pitfall: Forgetting to Narrow Union Types

The Problem: Not narrowing types leads to errors.

type Result = string | number;
function handle(result: Result) {
  console.log(result.toFixed(2)); // ❌ Error
}

The Fix: Use type guards.

function handle(result: Result) {
  if (typeof result === "number") {
    console.log(result.toFixed(2));
  }
}

4. Pitfall: Forgetting to Export Types Across Files

Always export types you intend to reuse.

// types.ts
export type User = { name: string };

5. Pitfall: Confusing interface vs type

Quick Rule of Thumb:

  • interface → Use for object shapes, extension, OOP-style patterns.
  • type → Use for unions, primitives, mapped types, aliases, and function signatures.

6. Pitfall: Over-Engineering with Complex Types

Avoid writing overly “smart” or recursive conditional types.

  • Start simple.
  • Use runtime helper functions instead of forcing everything into types.
  • Document trade-offs for clarity.

7. Pitfall: Not Using TypeScript Utility Types

Lean on built-in utilities like:

  • Partial<T>
  • Required<T>
  • Pick<T, K>
  • Omit<T, K>
  • Record<K, T>

8. Pitfall: Skipping strict Mode

Without strict mode, you’ll miss valuable errors. Always enable it:

{
  "compilerOptions": { "strict": true }
}

Includes checks like noImplicitAny, strictNullChecks, and more.

9. Pitfall: Ignoring .d.ts Files for External JS

When importing untyped JS modules:

// types/globals.d.ts
declare module 'my-js-lib';

Or, better yet, declare functions explicitly:

declare module 'my-js-lib' {
  export function doSomething(): void;
}

10. FAQ: Frequently Asked Questions

Why is TypeScript complaining about implicit any?

Because you’re missing a type in strict mode.

function log(msg: string) { ... }

Should I use React.FC?

Yes, for simple components, but avoid it for generics, default props, or contexts.

How do I type children in React?

type Props = { children: React.ReactNode };

Can I use TypeScript with JSON files?

Yes, set resolveJsonModule: true in tsconfig.json.

import data from './data.json';

Should I use Babel with TypeScript?

Yes, if you need faster builds or custom JSX transforms. But remember: Babel skips type checks, so still run tsc –noEmit.

Quick Recap

  • Avoid common mistakes like misusing any, skipping null checks, or over-engineering types.
  • Use strict mode and utility types to simplify your workflow.
  • Stick to simple, clear, and maintainable type designs.
  • Reference FAQs for quick solutions to recurring TypeScript questions.

From Zero to Production: Closing the Strictly Typed Series

Thank you for joining me on this full-stack TypeScript adventure. You’ve covered everything from TypeScript essentials and type safety to interfaces and structural typing, mastering functions & generics, OOP patterns and advanced typing, smooth integration into JavaScript projects, real-world usage with React & Next.js, and solid configuration & testing setups. Now, fully equipped with knowledge on production best practices, you’re set to build fast, scalable, and type-safe applications:

This journey equips you to confidently apply TypeScript in any setting. Wherever you are in your journey—building new applications, modernizing old ones, or optimizing production workflows—Strictly Typed leaves you ready to write cleaner, smarter, and safer code. Onward to mastering what’s next!

About the author

<a href="https://bitskingdom.com/blog/author/heberlyn/" target="_self">Heberlyn Mendoza</a>
Heberlyn Mendoza
I have been building my experience as a Full Stack Developer for around 5 years. I'm passionate about challenges, finding issues and blockers to explore my creativity and ingenuity, and then getting the satisfaction of getting things done. I always like to do different things, move on, and learn something different every day.

Explore more topics:

How to Design Pop-Ups Users Don’t Hate (And Actually Click)

A guide to keeping engagement high without driving people bananas.