Course contentsShow
← TypeScript
Lesson 996 of 1,79623. Type AssertionsFree lesson

Double Assertions as Red Flags

Recognize that double assertions often indicate design problems or missing type guards.

Double Assertions as Red Flags

What you'll learn: How to recognize when double assertions signal deeper problems in your type design or logic.

The Warning Sign

Double assertions (like value as unknown as SomeType) are TypeScript's equivalent of forcing a square peg into a round hole. While they technically work, they're almost always a red flag that something's wrong with your approach.

Why Double Assertions Appear

You typically reach for double assertions when a single assertion fails:

type Dog = { bark: () => void };
type Cat = { meow: () => void };

const dog: Dog = { bark: () => console.log("woof") };

// This fails - TypeScript won't allow incompatible assertions
// const cat = dog as Cat; // Error!

// So you force it through unknown
const cat = dog as unknown as Cat; // "Works" but dangerous

What They Really Mean

A double assertion usually indicates:

  1. You're working around missing type guards - If you properly narrowed with instanceof or type predicates, you wouldn't need the assertion
  2. Your types don't match reality - The actual data structure doesn't align with your type definitions
  3. You're abusing the type system - Trying to convince TypeScript of something that isn't true

Think of it like using duct tape to fix a structural problem in a building. Sure, it holds temporarily, but it's masking a real issue.

The Better Path Forward

When tempted to double assert, ask: "Why doesn't a single assertion work?" The answer usually points you toward the real solution - better type guards, corrected types, or restructured data.

Key Takeaway: Double assertions are a code smell signaling design problems or missing type guards. When you see as unknown as, pause and investigate the root cause instead of forcing the types.