Question
I am confused about the as const assertion in TypeScript. I have looked at documentation and videos, but I still do not fully understand it.
What does as const mean in the code below, and what is the benefit of using it?
const args = [8, 5] as const;
const angle = Math.atan2(...args);
console.log(angle);
Short Answer
By the end of this page, you will understand what as const does in TypeScript, how it changes literal inference, why arrays become readonly tuples, and when it is useful in everyday code. You will also see how it helps functions like Math.atan2(...args) type-check correctly.
Concept
as const is a const assertion in TypeScript. It tells TypeScript to infer the most specific, narrowest possible type for a value instead of a wider, more general one.
In practice, as const does three important things:
- It keeps literal values as literal types.
- It makes object properties
readonly. - It turns array literals into
readonlytuples.
Consider this example:
const args = [8, 5];
Without as const, TypeScript usually infers:
number[]
That means: “this is an array of numbers of any length.”
But with:
const args = [8, 5] as const;
TypeScript infers:
readonly [8, 5]
Mental Model
Think of TypeScript as trying to label boxes.
Without as const, if it sees this:
[8, 5]
it puts a broad label on the box:
- “This box contains numbers.”
It does not promise how many numbers there are.
With as const, TypeScript puts a much more exact label on the box:
- “This box contains exactly two items: first
8, then5, and nobody should change them.”
That extra precision is why spreading the array into a function works better. The function can trust the shape of the data.
So as const is like telling TypeScript:
- “Do not generalize this value.”
- “Keep it exactly as written.”
Syntax and Examples
The basic syntax is:
const value = expression as const;
Example 1: Literal values stay narrow
const status1 = "ok";
const status2 = "ok" as const;
status1 is often inferred as:
"ok"
But as const is especially useful in objects and arrays, where TypeScript would otherwise widen values more often.
Example 2: Object properties become readonly
const config = {
mode: "dark",
fontSize: 16,
} as const;
Type:
{
readonly mode: "dark";
readonly fontSize: ;
}
Step by Step Execution
Consider this code:
const args = [8, 5] as const;
const angle = Math.atan2(...args);
console.log(angle);
Step 1: Create the array literal
[8, 5]
This is an array literal with two numbers.
Step 2: Apply as const
const args = [8, 5] as const;
TypeScript infers the type as:
readonly [8, 5]
That means:
- first item is
8 - second item is
5 - there are exactly two items
- the tuple is readonly
Step 3: Spread the tuple into the function call
Real World Use Cases
as const is useful whenever a value should be treated as fixed and precise.
1. API request methods
const METHODS = ["GET", "POST", "PUT", "DELETE"] as const;
type HttpMethod = typeof METHODS[number];
This creates a safe union of allowed method names.
2. Redux-style action objects
const action = {
type: "INCREMENT",
} as const;
Now type is exactly "INCREMENT", which helps with action type checking.
3. Configuration objects
const settings = {
theme: "dark",
layout: "grid",
} as const;
Real Codebase Usage
In real projects, developers use as const to improve type safety without writing extra type definitions.
Common patterns
Guarding fixed values
const VALID_STATUSES = ["pending", "paid", "failed"] as const;
type Status = typeof VALID_STATUSES[number];
This keeps allowed values in one place.
Safer configuration
const APP_CONFIG = {
env: "production",
retries: 3,
} as const;
This prevents accidental mutation and preserves precise values.
Action creators
const setCount = (count: number) => ({
type: "SET_COUNT",
: count,
} );
Common Mistakes
1. Thinking as const is the same as const
These are different.
const name = "Sam";
const means the variable cannot be reassigned.
const user = { name: "Sam" };
You still can do this:
user.name = "Alex";
But with as const:
const user = { name: "Sam" } as const;
Now this is an error:
user.name = "Alex";
2. Using as const when you actually need mutability
Broken idea:
Comparisons
| Concept | What it does | Mutability | Example inferred type |
|---|---|---|---|
const x = ... | Prevents variable reassignment | Value may still be mutable | const arr = [1, 2] -> number[] |
as const | Preserves exact literal types | Readonly at type level | readonly [1, 2] |
| Explicit type annotation | You manually choose the type | Depends on the type | const arr: [number, number] = [1, 2] |
const vs as const
Cheat Sheet
Quick rules
as consttells TypeScript to keep a value as specific as possible.- String, number, and boolean literals stay literal.
- Object properties become
readonly. - Arrays become
readonlytuples.
Syntax
const value = expr as const;
Common examples
const status = "ok" as const;
// type: "ok"
const point = [10, 20] as const;
// type: readonly [10, 20]
const config = { theme: "dark" } as const;
// type: { readonly theme: "dark" }
Good use cases
- fixed lists of allowed values
- action type objects
- function argument tuples
- config objects that should not change
- deriving union types from arrays
Important note
FAQ
What does as const do in TypeScript?
It tells TypeScript to infer the narrowest possible type for a value, making literals exact, object properties readonly, and arrays readonly tuples.
Why is as const useful with arrays?
It turns an array literal into a readonly tuple, which is helpful when a function expects a fixed number of arguments.
Why does Math.atan2(...args) work better with as const?
Because Math.atan2 expects exactly two arguments, and as const tells TypeScript that args contains exactly two items.
Is as const the same as const?
No. const prevents variable reassignment. as const changes type inference to be more specific.
Does as const freeze objects at runtime?
No. It only affects TypeScript's compile-time type checking. It does not make the object truly immutable in JavaScript.
When should I avoid as const?
Avoid it when you need to mutate the object or array later, because it makes properties and tuple elements readonly.
Mini Project
Description
Build a small TypeScript utility for application statuses. The project demonstrates how as const helps define a fixed list of allowed values and safely validate user input against that list.
Goal
Create a type-safe status system using as const, then validate and print statuses without allowing invalid values.
Requirements
- Create a constant array of allowed statuses using
as const. - Derive a
Statustype from that array. - Write a function that checks whether a string is a valid status.
- Write a function that prints a message for a valid status.
- Test the code with both valid and invalid input.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular @ViewChild Returning Undefined: Lifecycle, Child Components, and Fixes
Learn why Angular @ViewChild can be undefined, when it becomes available, and how to access child components correctly using lifecycle hooks.