# Literal Types & const Assertions

> What are literal types in TypeScript? How does 'as const' work and when should you use it?

Canonical: https://www.internhack.in/learn/interview/typescript-interview/ts-literal-const
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

What are literal types in TypeScript? How does 'as const' work and when should you use it?

## Answer

Literal types represent exact values rather than broad categories. Instead of 'string', a literal type is a specific string like 'hello'. TypeScript automatically infers literal types for const declarations.

'as const' (const assertion) tells TypeScript to infer the narrowest possible type for an expression:

- Strings/numbers become literal types instead of string/number
- Arrays become readonly tuples instead of mutable arrays
- Objects get readonly properties with literal types

This is incredibly useful for configuration objects, action types, and anywhere you want TypeScript to track exact values.

## Code examples

### Literal Types

```
// String literal types
type Direction = 'north' | 'south' | 'east' | 'west';
let dir: Direction = 'north'; // OK
// dir = 'up'; // Error: Type '"up"' is not assignable to type 'Direction'

// Numeric literal types
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
function rollDice(): DiceRoll {
  return (Math.floor(Math.random() * 6) + 1) as DiceRoll;
}

// Boolean literal types
type True = true;
type False = false;

// let vs const inference
let mutable = 'hello';    // type: string (wide)
const immutable = 'hello'; // type: 'hello' (literal)

// Literal types in function parameters
function setAlignment(align: 'left' | 'center' | 'right'): void {
  document.body.style.textAlign = align;
}
setAlignment('center'); // OK
// setAlignment('top');  // Error
```

Literal types restrict values to exact matches. They are the foundation of discriminated unions and enable powerful pattern matching.

### const Assertions (as const)

```
// Without as const
const config = {
  endpoint: 'https://api.example.com',
  retries: 3,
  methods: ['GET', 'POST'],
};
// type: { endpoint: string; retries: number; methods: string[] }

// With as const
const configConst = {
  endpoint: 'https://api.example.com',
  retries: 3,
  methods: ['GET', 'POST'],
} as const;
// type: {
//   readonly endpoint: 'https://api.example.com';
//   readonly retries: 3;
//   readonly methods: readonly ['GET', 'POST'];
// }

// Useful for action types (Redux-style)
const ACTIONS = {
  INCREMENT: 'INCREMENT',
  DECREMENT: 'DECREMENT',
  RESET: 'RESET',
} as const;

type ActionType = typeof ACTIONS[keyof typeof ACTIONS];
// type: 'INCREMENT' | 'DECREMENT' | 'RESET'

// Without as const, ActionType would just be 'string'
```

as const freezes values to their literal types and makes everything readonly. It's essential for deriving union types from objects.

### Practical Patterns with as const

```
// Route definitions
const ROUTES = {
  home: '/',
  about: '/about',
  users: '/users',
  userDetail: '/users/:id',
} as const;

type Route = typeof ROUTES[keyof typeof ROUTES];
// '/' | '/about' | '/users' | '/users/:id'

function navigate(route: Route): void {
  window.location.href = route;
}
navigate(ROUTES.home); // OK
// navigate('/random');  // Error

// Tuple from as const
const rgb = [255, 128, 0] as const;
// type: readonly [255, 128, 0] (not number[])

// Enum-like pattern without enum keyword
const Status = {
  Active: 'active',
  Inactive: 'inactive',
  Pending: 'pending',
} as const;

type Status = typeof Status[keyof typeof Status];
// 'active' | 'inactive' | 'pending'

function setStatus(status: Status): void {
  console.log(`Setting status to: ${status}`);
}
setStatus(Status.Active); // OK
```

The 'as const + typeof + keyof' pattern is a popular alternative to enums. It provides the same type safety with less runtime overhead.

## Key points

- as const makes everything deeply readonly and narrows to literal types
- The typeof + keyof + as const combo creates union types from objects
- Literal types are erased at runtime - they only exist during type checking

## Common follow-up questions

- How does as const differ from Object.freeze()?
- Can you use as const with function return values?
- What is the satisfies operator and how does it work with as const?

## Interview tips

- Show the as const + typeof + keyof pattern for deriving union types
- Compare const enum vs as const object - both have trade-offs
- Mention that as const is compile-time only; Object.freeze is runtime

## Concepts covered

Literal Types, const Assertions, as const, Template Literals, Narrowing

Previous: [Union & Intersection Types](https://www.internhack.in/learn/interview/typescript-interview/ts-union-intersection) · Next: [unknown vs any vs never](https://www.internhack.in/learn/interview/typescript-interview/ts-unknown-any-never) · [All of TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)

## More TypeScript Interview questions

- [Type Assertions & Non-Null](https://www.internhack.in/learn/interview/typescript-interview/ts-type-assertions)
- [Discriminated Unions](https://www.internhack.in/learn/interview/typescript-interview/ts-discriminated-unions)
- [Mapped Types](https://www.internhack.in/learn/interview/typescript-interview/ts-mapped-types)
- [Conditional Types & infer](https://www.internhack.in/learn/interview/typescript-interview/ts-conditional-types)
- [Template Literal Types](https://www.internhack.in/learn/interview/typescript-interview/ts-template-literal)
- [Utility Types (Partial, Pick, etc.)](https://www.internhack.in/learn/interview/typescript-interview/ts-utility-types)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)
