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
| Operation | any | unknown |
|---|---|---|
| Can assign any values | yes | yes |
| Can perform any operations | yes | no (type must be checked) |
| Type safety | no | yes |
| Needs narrowing before use | no | yes |
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 stringTo 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
unknownis a supertype of all types, meaning anything can be assigned to it:
javascript
let x: unknown;
x = 123;
x = "string";
x = true;- But the reverse is not true - you cannot assign
unknownto 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- The
unknowntype is the "top" type of TypeScript's type system (an analog ofany, but safe):
javascript
never ⊂ string ⊂ unknownExample: 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
| Property | Description |
|---|---|
| Type | Primitive utility type |
| Purpose | Safe analog of any |
| Can be assigned | Any value |
| Can be used | Only after a type check |
| Typing | Strict and safe |
| Where it's used | Unknown 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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.