Double Assertion

What

A double assertion is a technique used to force a value into an entirely unrelated type by first asserting it to unknown.

How

You assert the value to unknown and then immediately to the target type:

const id = 42;
const userId = id as unknown as string; // Forced conversion

Why

TypeScript normally prevents you from asserting a type to one that has no overlap (e.g., number to string). Double assertion bypasses this check. While it can be useful in extreme edge cases when working with poorly typed legacy libraries, it is highly dangerous and almost always indicates a flaw in your program’s design.

Summary

Double assertion is the “trust me, bro” of TypeScript taken to the extreme—use it as a last resort, if at all.