In TypeScript, there is a significant difference between the two statements:
clientLoader.hydrate = true as const;
clientLoader.hydrate = true;
FYI I have picked these example from React router v7.
Let’s break down the difference with detailed explanations and examples
clientLoader.hydrate = true as const;
The as const
assertion in TypeScript is a way of telling the TypeScript compiler to treat a value as its literal type, rather than the general type.
In this case, true as const
means that the type of hydrate
will be the literal true
and not boolean
. It essentially locks the value of hydrate to true
specifically.
Example:
interface ClientLoader {
hydrate: true; // The type of hydrate is set to the literal value `true`
}
const clientLoader: ClientLoader = {
hydrate: true as const,
};
clientLoader.hydrate = true; // This is valid
// clientLoader.hydrate = false; // Error: Type 'false' is not assignable to type 'true'
In the above example,
clientLoader.hydrate
is specifically typed astrue
. You cannot assign any value other thantrue
tohydrate
because of theas const
assertion.This type of assignment is useful when you want to enforce immutability for certain properties.
clientLoader.hydrate = true;
Without the as const
assertion, TypeScript will infer the type of hydrate
as boolean
. This means that hydrate
can be assigned any boolean
value (true or false)
.
Example:
interface ClientLoader {
hydrate: boolean; // The type of hydrate is set to `boolean`
}
const clientLoader: ClientLoader = {
hydrate: true,
};
clientLoader.hydrate = true; // This is valid
clientLoader.hydrate = false; // This is also valid
In this case, since
hydrate
is typed asboolean
, you can assigntrue
orfalse
to it.It provides flexibility to switch between both
true
andfalse
.
Feature | clientLoader.hydrate = true as const; |
clientLoader.hydrate = true; |
---|---|---|
Type of hydrate |
true (literal type) |
boolean (general type) |
Flexibility | Can only be true
|
Can be true or false
|
Use Case | When you want the property to be strictly true and not allow changes |
When the property can hold any boolean value |
Type Inference | The type of hydrate is narrowed to true
|
The type of hydrate is inferred as boolean
|
Why Use as const?
Enforcing Immutability:
as const
locks down the value so that it can’t be changed to something else. This is particularly useful when you want to ensure that a specific value is always the same throughout the program.Literal Types for Discriminated Unions: When working with discriminated unions,
as const
allows you to create specific cases based on literal types, as shown below:
type Status = 'pending' | 'completed' | 'failed';
const status: Status = 'pending'; // 'pending' is a valid value
// status = 'in-progress'; // Error: Type '"in-progress"' is not assignable to type 'Status'
// With 'as const'
const taskStatus = 'pending' as const;
// Now taskStatus is narrowed down to the literal type 'pending'
Conclusion
Use
as const
when you want to assign a specific literal value to a property and enforce that the value remains fixed.Use regular assignment
(true, false, etc.)
when you want to allow a property to accept differentboolean
values or when the exact value doesn’t need to be restricted.
This makes as const a useful tool for more precise typing and enforcing stricter value constraints in your TypeScript code.
Top comments (0)