The unknown Type
A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
unknown looks a lot like any at first glance — a variable typed unknown can also be assigned any value at all. The difference is what happens next: TypeScript refuses to let you do anything with an unknown value until you've proven what type it actually is.
let value: unknown = 4; value = "hello"; // fine — unknown accepts any value, just like any value = true; // still fine
Try to call a method or read a property on an unknown value, and TypeScript stops you cold. It won't assume value is a string just because you happened to assign a string to it a moment ago — as far as the type system is concerned, it could be anything.
let value: unknown = "hello"; console.log(value.toUpperCase()); // Error: Object is of type 'unknown'.
Narrowing means proving the type with a runtime check — typeof, instanceof, or a custom type guard — before you use the value. Once TypeScript sees that check, it trusts the narrowed type inside that branch and lets you use it normally.
let value: unknown = "hello";
if (typeof value === "string") {
console.log(value.toUpperCase()); // fine — TypeScript knows it's a string here
} else {
console.log("not a string");
}Before you can call a method on a value typed unknown, you must first ____ its type with a check like typeof.
What is the key advantage of unknown over any for a value of uncertain type?
let value: unknown = 5; console.log(value + 1); compiles without any type narrowing.
Keep your streak going in Swipe Coding
Swipe through every level of TypeScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →