Search⌘ K
AI Features

The `unknown` Type

Explore the unknown type in TypeScript as a safer substitute for any. Understand type assignability rules, how unknown prevents unsafe operations without casting, and why it’s essential for handling uncertain external data. This lesson helps you write safer, more reliable TypeScript by minimizing the risks of implicit any usage.

Overview

When trying to eliminate any from your codebase, it’s useful to know about the unknown type. It is a safer alternative to any. Both any and unknown represent an unknown type. However, there is a key difference between these two:

  • all types are assignable to the any type and the any type is assignable to any other type
  • all types are assignable to the unknown type, but the unknown type is not assignable to any type
anyunknownassignable toall typesno typesassignable fromall typesall types

Type assignability

What ...