The Special Types: any, unknown, never, void

Beginner · 18 min read · ▶ live playground · ✦ checkpoint

any, unknown, never, and void are TypeScript's special types: they do not describe normal value sets like string or number. any turns checking off, unknown keeps checking on until you prove more, never has no values, and void marks a function whose return value should not be used.

These types show up at the edges of your code. Treat them as warning labels, not decorations.

any: the off switch

any is the type that tells TypeScript to stop checking a value. It is not "a flexible type." It is the checker stepping away from that value and whatever you build from it.

That is why any is contagious, meaning it spreads through code that uses it:

const rawPlan: any = 42;
const label = rawPlan.toUpperCase();
const message: string = label;
 
console.log(message);

→ TypeError: rawPlan.toUpperCase is not a function

The checker let all of that through. rawPlan was any, so rawPlan.toUpperCase() was allowed. Then label became any, so assigning it to a promised string was allowed too. The crash happened only at run time.

unknown: the honest twin

unknown means "I have a value, but I do not know its type yet." It is the honest twin of any: both can hold anything, but unknown fails loud instead of silent.

Try the same move with unknown:

const rawPlan: unknown = 42;
const label = rawPlan.toUpperCase();

→ playground.ts:2:15 - error TS18046: 'rawPlan' is of type 'unknown'.

That error is the feature. unknown is a sealed box: you can pass it around, but you cannot use what is inside until you inspect it. A light typeof check is enough for primitive values; lesson 4.2 teaches how TypeScript follows those checks through branches.

typescript — playgroundtype-checked
⌘/Ctrl + Enter to run

never: the empty set

never is the type with no possible values. If types are sets of allowed values, never is the empty set.

You cannot put a value into it:

function stopLaunch(reason: string): never {
  throw new Error(reason);
}
 
const impossible: never = "launch";

→ playground.ts:5:7 - error TS2322: Type '"launch"' is not assignable to type 'never'.

So why keep a type with no values? Because it lets TypeScript describe code paths that cannot produce a value: a function that always throws, a branch that cannot happen, or a state you have fully ruled out. An exhaustiveness check is a pattern that proves every case was handled; it uses never later, starting in lesson 4.3 and hardening in 15.2.

void: no useful return value

void is the return type for a function whose return value should not be used. It does not mean "no code ran." It means "do not expect a meaningful result back."

typescript — playgroundtype-checked
⌘/Ctrl + Enter to run

→ Mina recorded a launch.
→ undefined

The function ran. It logged the launch. Its return value is not useful, so TypeScript gives it void. At run time, JavaScript still produces undefined when a function reaches the end without returning a value.

You now have the four special types: the off switch, the sealed box, the empty set, and the ignored return. Section 3 turns to functions, where these types start appearing at boundaries: parameters, returns, functions passed into other functions, and promises made to callers.

Checkpoint

Answer all three to mark this lesson complete

+50 XP on completion