...

/

The `unknown` Type

The `unknown` Type

This lesson explains the `unknown` type and explains how it is a better alternative to using `any`.

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 does it mean when a type is assignable to another type? We say that type A is assignable to type B if you ...