# Literal Types

> Constraining values to exact specific strings or numbers.

Subject: TypeScript | Lesson 9 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/literal-types
Previous: [Nullish Values & !](https://codewithanimation.in/typescript/nullish-values.md) | Next: [Union Types](https://codewithanimation.in/typescript/union-types.md)

## Beyond 'string'

Often, typing a variable as `string` or `number` is far too broad. If you are building a Button component, you don't want the user to pass *any* string as a size, you only want them to pass specific strings.

A Literal Type allows you to specify the exact value that a variable is allowed to hold.

## Autocomplete and safety

Using literal types completely changes the developer experience. If a property is typed as `string`, the developer has to open the documentation to figure out what strings are allowed.

If the property is typed as `'sm' | 'md' | 'lg'`, their IDE will automatically suggest those three exact strings as they type, and will throw a compiler error if they try to pass `'xl'`.

## Defining a Literal Union

You define a literal type just by typing the exact string, number, or boolean value you want. Usually, you combine multiple literals using a union (`|`):

```typescript
type ButtonSize = "sm" | "md" | "lg";

let size: ButtonSize = "md"; // OK
size = "xl"; // Error: Type '"xl"' is not assignable to type 'ButtonSize'.
```

## Common mistake: The let inference trap

A common point of confusion happens when you try to pass an object's property to a function expecting a literal type:

```typescript
const obj = { size: "md" };
// Error! obj.size is inferred as 'string', not '"md"'
renderButton(obj.size);
```

TypeScript infers object properties as general types (like `string`) because objects are mutable. You have to assert the property using `as const` to lock it to a literal type.

## Tip: Boolean literals

The `boolean` type in TypeScript is actually just an alias for a union of two literal types: `true | false`.

## Try it: Try it

Try typing an invalid size string into the input field to see how the compiler instantly rejects it.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/literal-types

## Check yourself

### Question 1

Why would you use a string literal type instead of the general 'string' type?

- A. To compress the compiled JavaScript output size.
- B. To allow the variable to accept numbers that look like strings.
- C. To restrict a variable to a specific, predefined set of valid string values.

Correct answer: C

Literal types narrow the infinite possibilities of a 'string' down to exactly the handful of values your program actually supports.

- Why not A: Literal types do not affect compiled JavaScript.
- Why not B: No, literal types are strictly enforced.

## Remember this

- A Literal Type specifies an exact, specific value (like `'sm'`).
- Combining them in unions (`'sm' | 'md'`) creates powerful constraints.
- They give other developers instant autocomplete for valid options.
- Object properties infer as general strings unless locked with `as const`.
