Why You Should Use as const in TypeScript

By Tomer Erusalimsky

January 14, 20251 min read

TypeScript
JavaScript
Developer Tips

The as const assertion is a small feature with big benefits. Here's how it can improve your TypeScript code:

Literal Types, Not Generalized Ones

const color = "blue"; // type: string const color = "blue" as const; // type: "blue"

✅ Get precise literal types instead of broad generic types

Immutable Arrays and Objects

const roles = ["admin", "user"] as const; // type: readonly ["admin", "user"]

✅ Create read-only data structures that TypeScript can reason about

Cleaner Discriminated Unions

const statuses = { SUCCESS: "success", ERROR: "error", } as const; type Status = typeof statuses[keyof typeof statuses]; // type: "success" | "error"

✅ Build type-safe enums and union types from objects

Better Autocompletion & Type Safety

By narrowing types, as const helps TypeScript provide smarter suggestions and catch bugs earlier.

Pro Tip

💡 Use as const when you want precise, read-only, and narrow types. It's especially handy for:

  • Enums
  • Config objects
  • Action types
  • Any data that shouldn't change

The as const assertion is a powerful tool for creating more robust and type-safe TypeScript applications with minimal effort.