Typescript Foundations / Basic Types

Literal Types

👉 A literal type is a type that represents a specific exact value, instead of a general category like string, number, or boolean.

Examples:

  • "yes" is a string literal type.

  • 42 is a number literal type.

  • true is a boolean literal type.

They help enforce specific allowed values.


✅ Summary

Literal Type

Example

String literal

"on", "off"

Number literal

1, 2, 100

Boolean literal

true, false

Union of literals

`"yes"

✅ Use literal types when you want to strictly control values.

✅ Combine them with union, type, const, and as const for powerful patterns.