Course contentsShow
← TypeScript
Lesson 477 of 1,79611. Literal TypesFree lesson

When Not to Use as const

Cases where as const is too restrictive or interferes with intended type flexibility.

When Not to Use as const

You'll learn: When as const makes your code too rigid and how to recognize situations where you need flexibility instead.

The Right Tool for the Wrong Job

as const is powerful — it locks down your values to their exact literal types. But sometimes you want TypeScript to widen types, or you need values to be reassignable. Using as const in these situations creates unnecessary friction.

Think of as const like shrink-wrapping food: great for preservation, terrible when you need to add ingredients later.

Cases Where as const Backfires

You Need to Mutate Values

const colors = ['red', 'green', 'blue'] as const;
// Type: readonly ['red', 'green', 'blue']

colors.push('yellow'); // ❌ Error: push doesn't exist on readonly array

If you plan to modify an array, don't use as const.

You Want General Types

const config = {
  timeout: 5000,
  retries: 3
} as const;
// Type: { readonly timeout: 5000; readonly retries: 3 }

function setConfig(options: { timeout: number; retries: number }) {
  // ...
}

setConfig(config); // ✅ Works, but overly specific

Here as const works, but it's unnecessarily restrictive. If you just need number types, let TypeScript infer normally.

Function Parameter Flexibility

const defaultPort = 8080 as const; // Type: 8080

function connect(port: number) {
  // accepts any number
}

connect(defaultPort); // ✅ Works (8080 is assignable to number)
connect(9000); // ✅ Also works

This is fine, but as const adds no value here — number annotation alone is clearer.

Working with External APIs

const apiPayload = {
  action: 'create'
} as const;

// If the API expects { action: string }, the literal 'create' works
// but limits your ability to build dynamic payloads

Key Takeaway: Only use as const when you truly need readonly, literal-type precision. For mutable data, dynamic values, or when broader types improve flexibility, skip it and let TypeScript infer naturally.