Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Special Types · Lesson 11 of 12

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

🤖
Looks like any at first — wait for the twist. 👀

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
So far this looks identical to any — the difference only shows up when you try to use the value.

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'.
Even though value clearly holds a string right now, TypeScript won't let you call .toUpperCase() on an unknown-typed variable without first narrowing its type.
🤖
Narrowing is basically how you earn TypeScript's trust back. 🤝

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");
}
Inside the typeof check, TypeScript narrows value from unknown down to string, so .toUpperCase() is now safe to call.
Fill in the blank

Before you can call a method on a value typed unknown, you must first ____ its type with a check like typeof.

Quick check

What is the key advantage of unknown over any for a value of uncertain type?

True or false

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 →