Visit complete TypeScript roadmap
TypeScript Topic

Literal Types

Literal Types

Literal types in TypeScript are a way to specify a value exactly, rather than just a type. Literal types can be used to enforce that a value must be of a specific type and a specific value. Literal types are created by using a literal value, such as a string, number, or boolean, as a type.

For example, the following is a literal type that represents a value of 42:

type Age = 42;

let age: Age = 42; // ok
let age: Age = 43; // error

In this example, the Age literal type is created by using the number 42 as a type. This type can then be used to enforce that a value must be of type number and have the value 42.

Learn more from the following links:

More Topics

Explore related content

View All Topics
Loved by 100K+ Developers

Start Your Learning
Journey Today

Join thousands of developers who are leveling up their skills with structured roadmaps and expert guidance

No credit card required
Always free
Track your progress