Back to Blog
TypeScriptconstLiteral TypesImmutability

TypeScript const Assertions Guide

Master TypeScript const assertions for literal types, immutable objects, and readonly arrays.

B
Bootspring Team
Engineering
December 21, 2019
7 min read

The as const assertion creates the narrowest possible type from a value. Here's how to use it.

Basic Usage

Loading code block...

Arrays and Tuples

Loading code block...

Enum-like Constants

Loading code block...

Immutable Objects

Loading code block...

Function Parameters

Loading code block...

Route Definitions

Loading code block...

Action Types (Redux-like)

Loading code block...

Validation Schemas

Loading code block...

Event Maps

Loading code block...

Combining with satisfies

Loading code block...

Best Practices

Usage: ✓ Use for enum-like constants ✓ Use for configuration objects ✓ Use for route definitions ✓ Use for action types Benefits: ✓ Narrowest possible types ✓ Deep readonly ✓ Literal type inference ✓ Better autocomplete Patterns: ✓ Combine with typeof ✓ Extract union types ✓ Use with satisfies ✓ Create type-safe enums Avoid: ✗ On mutable data ✗ When you need assignment ✗ For temporary values ✗ Overusing everywhere

Conclusion

The as const assertion creates the narrowest literal types and makes values deeply readonly. Use it for configuration, constants, enum alternatives, and action types. Combine with typeof and keyof to extract useful types, and use satisfies for validation while preserving literal types.

Share this article

Help spread the word about Bootspring

Related articles