Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
TypeScript Interfaces  ›  Lesson

any, unknown and never

unknown 12 minany turns checking off, unknown accepts anything but must be narrowed before use, and never is the type with no values at all.

any, unknown and never

Three special types sit at the edges of TypeScript's type system. any and unknown both accept every value, yet one is dangerous and the other is safe. never accepts no value at all, and is surprisingly useful. Knowing which is which keeps your checks honest.

The rule

any trusts everything, unknown trusts nothing yet, never has no values.

any — accepts every value and allows every operation. Checking is switched off:

let saved: any = JSON.parse(text);
saved.tasks.forEach((t: any) => t.titel.trim()); // compiles, may crash
const n: number = saved;                          // also compiles

any also leaks: assign it to a typed variable and the compiler believes you.

unknown — also accepts every value, but allows nothing until you check what it is:

let data: unknown = JSON.parse(text);
data.tasks;                     // error: data is unknown

if (typeof data === "object" && data !== null && "tasks" in data) {
  // data is now known to be an object with a tasks property
}

unknown is the right type for things that come from outside — parsed JSON, user input, caught errors (catch (e) is unknown in strict mode). You pay for safety with a check, which is exactly the point.

never — the type with no values. You meet it in three places:

  • A function that never finishes normally, such as one that always throws: function fail(msg: string): never { throw new Error(msg); }
  • Impossible types, like string & number.
  • What is left after narrowing has ruled out every possibility — which gives an exhaustive check:
type Status = "todo" | "doing" | "done";

function label(s: Status): string {
  switch (s) {
    case "todo":  return "To do";
    case "doing": return "In progress";
    case "done":  return "Done";
    default:
      const unreachable: never = s;
      return unreachable;
  }
}

Every case is handled, so in default the type of s is never. Add "blocked" to Status and forget a case: now s could be "blocked", which cannot be assigned to never, and the compiler points straight at the missing case.

The shortcut

Ask "How much do I know about this value?"

  • Nothing yet, it comes from outside → unknown, then narrow.
  • I am giving up on checking → any, and only as a last resort.
  • This cannot happen → never.
Loading saved tasksStep 1 of 4
  1. Parse

    const raw: unknown = JSON.parse(localStorage.getItem("tasks") ?? "[]");

Drills

Answer fast — trust the rule.

quick drill

A value is typed unknown. What can you do with it before any check?

quick drill

Which type is safest for the result of JSON.parse?

quick drill

What is the return type of a function that always throws an error?

quick drill

In the exhaustive switch over Status, what happens if you add "blocked" to Status but no new case?

quick drill

Why is any considered more dangerous than unknown?

Remember

  • any trusts everything, unknown trusts nothing yet, never has no values.
  • Ask how much you know: nothing yet is unknown; impossible is never.
  • Trap: any compiles typos and leaks into typed code; reach for unknown first.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThree special types

Explain the difference between any, unknown and never using one example each from a task tracker, for instance reading saved data, handling an error, and checking that every Status is handled in a switch.