capypad
0 day streak
typescript / beginner
Snippet

Literal Types

A literal type allows you to specify the exact value a variable must hold. This is useful for variables that should only represent specific fixed values.

snippet.ts
typescript
1
2
let status: "active" = "active";
// status = "inactive"; // Error
Breakdown
1
status: "active"
The variable is typed as the literal string 'active', meaning it cannot hold any other string.
2
// status = "inactive";
If you try to assign a different value, the TypeScript compiler will report an error.