Home > Web Front-end > JS Tutorial > Mastering Literal Types in TypeScript: true as const vs true

Mastering Literal Types in TypeScript: true as const vs true

Linda Hamilton
Release: 2024-12-20 19:21:11
Original
404 people have browsed it

Mastering Literal Types in TypeScript: true as const vs true

In TypeScript, there is a significant difference between the two statements:

  1. clientLoader.hydrate = true as const;

  2. clientLoader.hydrate = true;

FYI I have picked these example from React router v7.

Let’s break down the difference with detailed explanations and examples

  1. 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'
Copy after login
Copy after login
  • 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.

  1. 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
Copy after login
  • 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

Why Use as const?

  1. 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.

  2. 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'
Copy after login
Copy after login

Conclusion

  1. Use as const when you want to assign a specific literal value to a property and enforce that the value remains fixed.

  2. 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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template