Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Assertions and Type Compatibility  ›  Lesson

as any and as const

as const 12 minas any switches type checking off for a value, while as const locks a value down to its narrowest, read-only literal type.

as any and as const

Two assertions look alike and do opposite things. as any tells the compiler to stop checking a value. as const tells it to check the value more precisely. One should be rare, the other you will reach for often.

The rule

as any switches checking off; as const locks values down.

as any turns a value into type any, which allows every operation:

const data = response as any;
data.tasks.map((t: any) => t.titel.toUpperCase()); // compiles

The typo titel sails through, and so would calling something that is not a function. Checking resumes only when the value is assigned to something with a real type. as any is an escape hatch for moments when the types are genuinely wrong or unavailable. It spreads quietly, so keep it rare and local, and prefer unknown (covered in the last section) when you just do not know the type yet.

as const is a const assertion. It makes the compiler infer the narrowest type it can:

const statuses = ["todo", "doing", "done"] as const;
// type: readonly ["todo", "doing", "done"]

statuses[0];          // "todo", not just string
statuses.push("x");   // error: readonly

const defaults = { status: "todo", done: false } as const;
// type: { readonly status: "todo"; readonly done: false }

Three effects, all at once:

  1. Literal types — "todo" stays "todo" instead of widening to string.
  2. readonly — properties and elements cannot be reassigned.
  3. Tuples — an array literal becomes a fixed, read-only tuple.

A handy pattern: build a union type from a constant list, so the values exist at runtime and as a type:

type Status = (typeof statuses)[number]; // "todo" or "doing" or "done"

Like every assertion, both are erased: the output JavaScript is the same as without them.

The shortcut

Ask "Am I adding information or removing it?" as const adds precision the compiler can check. as any removes checking. Adding is almost always welcome; removing needs a reason you could explain in a code review.

Building the status listStep 1 of 4
  1. Plain array

    const statuses = ["todo", "doing", "done"]; is inferred as string[]. Any string could be pushed in.

Drills

Answer fast — trust the rule.

quick drill

What is the type of const s = ["a", "b"] as const;?

quick drill

After const d = response as any;, what does d.nothing.here() do at compile time?

quick drill

With const cfg = { mode: "dark" } as const;, what happens on cfg.mode = "light";?

quick drill

Which statement about as const and as any is true?

Remember

  • as any switches checking off; as const locks values down.
  • Ask "am I adding information or removing it?"
  • Trap: as any spreads silently — keep it rare and local, and try unknown first.

Practice — level up

0/1 solved

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

ExplainTwo very different assertions

Compare const statuses = ["todo", "done"] as const; with const data = response as any;. Explain what type each produces and why one is generally encouraged and the other avoided.