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.
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 as true. You cannot assign any value other than true to hydrate because of the as const assertion.
This type of assignment is useful when you want to enforce immutability for certain properties.
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 as boolean, you can assign true or false to it.
It provides flexibility to switch between both true and false.
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 |
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:
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'
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 different boolean 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.
The above is the detailed content of Mastering Literal Types in TypeScript: true as const vs true. For more information, please follow other related articles on the PHP Chinese website!