Skip to content

Unknown Type

In one line

The unknown type accepts any value but requires type narrowing before use, making it a type-safe alternative to any.

In simple words

The unknown type is the type-safe version of any. Like any, it accepts any value. Unlike any, you cannot use the value without first narrowing the type — you must check it with typeof, instanceof, or a type guard.

This makes unknown much safer for handling untrusted data like JSON.parse results, API responses, and user input. It forces you to verify the type before using it, preventing runtime errors.

Prefer unknown over any whenever possible. Use any only when you genuinely need to opt out of type checking (which should be rare). The unknown type was added in TypeScript 3.0.

Example

typescript
const data: unknown = JSON.parse("{}");
// data.name; // Error: Object is of type 'unknown'

if (typeof data === "object" && data !== null) {
  console.log(data); // OK: narrowed to object
}

unknown requires narrowing before use, unlike any which allows all operations.

Common confusions

  • Confused with: any

    The difference: any allows all operations without checking; unknown requires type narrowing first. unknown is always safer.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary