Skip to main content

What does the unknown type mean?

What the unknown type means in TypeScript

The unknown type is a safe alternative to any. It means:

"The type of the value is not known in advance, and before using it you need to check the type (type narrowing)."

In other words, unknown tells TypeScript:

"I don't know what this value is, but I want to work with it safely."


Basic usage example

javascript
let value: unknown; value = 42; value = "Hello"; value = { name: "Tim" }; value = true;

So you can assign unknown any value, but you cannot use it without checking.


How it differs from any

Operationanyunknown
Can assign any valuesyesyes
Can perform any operationsyesno (type must be checked)
Type safetynoyes
Needs narrowing before usenoyes

Example:

javascript
let a: any = 10; a.toUpperCase(); // the error will only appear at runtime let b: unknown = 10; // b.toUpperCase(); // compile-time error - TypeScript does not know this is a string

To use unknown, you must first check the type:

javascript
if (typeof b === "string") { console.log(b.toUpperCase()); // now it's fine }

Examples of using unknown

1. When working with external data (for example, an API)

javascript
function parseJSON(input: string): unknown { return JSON.parse(input); } const result = parseJSON('{"name": "Tim"}'); // console.log(result.name); // not allowed if (typeof result === "object" && result !== null && "name" in result) { console.log((result as { name: string }).name); // safe }

2. With dynamic data types

javascript
function handleInput(data: unknown) { if (typeof data === "number") { console.log(data.toFixed(2)); } else if (typeof data === "string") { console.log(data.toUpperCase()); } else { console.log("Unknown type"); } }

Features of the unknown type

  1. unknown is a supertype of all types, meaning anything can be assigned to it:
javascript
let x: unknown; x = 123; x = "string"; x = true;
  1. But the reverse is not true - you cannot assign unknown to a variable of another type without a check:
javascript
let y: number; let x: unknown = 10; // y = x; // error - the type must be explicitly narrowed or cast y = x as number; // correct
  1. The unknown type is the "top" type of TypeScript's type system (an analog of any, but safe):
javascript
never ⊂ string ⊂ unknown

Example: comparing unknown and any

javascript
function testAny(value: any) { value.foo.bar(); // compiles, but blows up at runtime } function testUnknown(value: unknown) { // value.foo.bar(); // compile-time error - safe if (typeof value === "object" && value !== null) { console.log("Object:", value); } }

Quick summary

PropertyDescription
TypePrimitive utility type
PurposeSafe analog of any
Can be assignedAny value
Can be usedOnly after a type check
TypingStrict and safe
Where it's usedUnknown data (API, JSON.parse, user input)

Summary

  • any → "Do whatever you want, TypeScript doesn't check."
  • unknown → "You can store anything, but prove it's safe."

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.