Lesson 38 of 60 – Literal Types
63%

Literal Types in TypeScript

Literal types allow you to specify exact values that a variable, property, or parameter can contain. Instead of allowing every string or number, a literal type restricts the value to specific values.

Note: Literal types are useful when you want to allow only a small, predefined set of values.

1. What is a Literal Type?

A literal type represents one exact value.

let status: "active";

status = "active";

The variable can contain only the exact string "active".

2. String Literal Type

A string literal type restricts a variable to one exact string.

let direction: "left";

direction = "left";

// direction = "right"; // Error

3. Multiple String Literal Types

Multiple literal values can be combined using a union.

let direction:
    "left" |
    "right";

direction = "left";

direction = "right";

Only "left" or "right" is allowed.

4. Literal Type with Status

Literal types are commonly used for status values.

let status:
    "pending" |
    "approved" |
    "rejected";

status = "pending";

status = "approved";

5. Invalid Literal Value

A value that is not included in the literal type is rejected.

let status:
    "pending" |
    "approved" |
    "rejected";

status = "pending";

// status = "completed"; // Error

6. Number Literal Type

Literal types can also represent exact numbers.

let score: 100;

score = 100;

// score = 90; // Error

7. Multiple Number Literals

Multiple number literals can be combined with a union.

let rating:
    1 |
    2 |
    3 |
    4 |
    5;

rating = 5;

rating = 3;

Only the specified numbers are accepted.

8. Boolean Literal Type

Boolean literal types can represent exactly true or false.

let active: true;

active = true;

// active = false; // Error

9. true or false Literal Union

Both boolean values can be allowed using a union.

let active:
    true |
    false;

active = true;

active = false;

In practice, boolean is usually used when both values are allowed.

10. Literal Types with Variables

Literal types can be used directly with variables.

let color:
    "red" |
    "green" |
    "blue";

color = "red";

color = "blue";

11. Literal Types in Function Parameters

Function parameters can accept only specific literal values.

function move(
    direction:
        "left" |
        "right"
) {

    console.log(
        direction
    );
}

move("left");

move("right");

12. Invalid Function Argument

TypeScript rejects arguments that are not part of the literal type.

function move(
    direction:
        "left" |
        "right"
) {
    console.log(direction);
}

move("left");

// move("up"); // Error

13. Literal Types in Function Return Types

A function can be required to return one of several specific literal values.

function getStatus():
    "success" |
    "error" {

    return "success";
}

console.log(
    getStatus()
);

14. Literal Types in Interface Properties

Interfaces can use literal types for their properties.

interface User {
    name: string;

    role:
        "admin" |
        "student";
}

const user: User = {
    name: "Amit",
    role: "student"
};

15. Literal Type for User Roles

Literal types are useful for defining fixed user roles.

type Role =
    "admin" |
    "teacher" |
    "student";

let role: Role;

role = "admin";

role = "teacher";

role = "student";

16. Literal Type for Course Level

Literal types can represent fixed course levels.

type Level =
    "beginner" |
    "intermediate" |
    "advanced";

let level: Level;

level = "beginner";

level = "advanced";

17. Literal Type for HTTP Methods

Literal types can restrict an HTTP method to known values.

type HttpMethod =
    "GET" |
    "POST" |
    "PUT" |
    "DELETE";

let method: HttpMethod;

method = "GET";

method = "POST";

18. Literal Type for Button Variant

Literal types are useful for UI component variants.

type ButtonVariant =
    "primary" |
    "secondary" |
    "danger";

let variant: ButtonVariant;

variant = "primary";

variant = "danger";

19. Literal Types in React Props

React component props can use literal types to restrict accepted values.

interface ButtonProps {
    title: string;

    variant:
        "primary" |
        "secondary" |
        "danger";
}

function Button(
    props: ButtonProps
) {

    return (
        <button>
            {props.title}
        </button>
    );
}

Button({
    title: "Save",
    variant: "primary"
});

20. Literal Types in React Native

React Native components can use literal types for component variants.

interface ButtonProps {
    title: string;

    variant:
        "primary" |
        "secondary" |
        "danger";
}

const Button = (
    props: ButtonProps
) => {

    return (
        <View>
            <Text>
                {props.title}
            </Text>
        </View>
    );
};

21. Literal Types and Type Aliases

Type aliases make repeated literal type definitions easier to manage.

type Status =
    "pending" |
    "approved" |
    "rejected";

let status1: Status =
    "pending";

let status2: Status =
    "approved";

22. Literal Types and Type Narrowing

Literal values can help TypeScript narrow the possible structure of an object.

interface Admin {
    role: "admin";
    name: string;
}

interface Student {
    role: "student";
    name: string;
}

type User =
    Admin | Student;

function showUser(
    user: User
) {

    if (user.role === "admin") {

        console.log(
            "Admin:",
            user.name
        );

    } else {

        console.log(
            "Student:",
            user.name
        );

    }
}

23. Discriminated Unions

A literal property can act as a discriminator between different object types.

interface Success {
    status: "success";
    data: string;
}

interface ErrorResponse {
    status: "error";
    message: string;
}

type Response =
    Success |
    ErrorResponse;

function handleResponse(
    response: Response
) {

    if (
        response.status ===
        "success"
    ) {

        console.log(
            response.data
        );

    } else {

        console.log(
            response.message
        );

    }
}

24. Literal Types with const

A const variable can preserve a literal value in situations where a let variable may be widened.

const status =
    "success";

let message =
    "success";

Literal inference can be useful when working with fixed values.

25. Literal Types for API Status

API response states can be represented using literal types.

type ApiStatus =
    "loading" |
    "success" |
    "error";

let status: ApiStatus =
    "loading";

status = "success";

status = "error";

26. Common Literal Type Mistake

A common mistake is assigning a value that is not part of the literal union.

type Status =
    "active" |
    "inactive";

let status: Status;

status = "active";

// status = "pending"; // Error

Only the values declared in the literal type are accepted.

27. When Should You Use Literal Types?

Literal types are useful when the possible values are known and limited.

  • User roles
  • Order statuses
  • Course levels
  • Button variants
  • HTTP methods
  • Theme names
  • API states
  • Direction values

28. Literal Types vs General Types

A general type accepts many values, while a literal type accepts only specific values.

let color: string;

color = "red";
color = "green";
color = "anything";

let fixedColor:
    "red" |
    "green";

fixedColor = "red";
fixedColor = "green";

// fixedColor = "blue"; // Error

29. Complete Practical Example

type Role =
    "admin" |
    "teacher" |
    "student";

type Status =
    "active" |
    "inactive";

type Level =
    "beginner" |
    "intermediate" |
    "advanced";

interface User {
    id: number;
    name: string;
    role: Role;
    status: Status;
    level: Level;
}

const user: User = {
    id: 101,
    name: "Amit",
    role: "student",
    status: "active",
    level: "beginner"
};

function showUser(
    user: User
) {

    console.log(
        "Name:",
        user.name
    );

    console.log(
        "Role:",
        user.role
    );

    console.log(
        "Status:",
        user.status
    );

    console.log(
        "Level:",
        user.level
    );
}

showUser(user);

30. Literal Types Summary

Literal types allow TypeScript to restrict values to specific strings, numbers, booleans, or combinations of those values.

type Status =
    "pending" |
    "approved" |
    "rejected";

let status: Status =
    "pending";

They are especially useful for fixed choices, component props, API states, and discriminated unions.

📌 Key Points

  • A literal type represents an exact value.
  • String literals restrict values to specific strings.
  • Number literals restrict values to specific numbers.
  • Boolean literals can represent true or false.
  • Multiple literal values can be combined with a union.
  • Literal types can be used with variables.
  • Function parameters and return types can use literal types.
  • Interface properties can use literal types.
  • Literal types are useful for React and React Native props.
  • They are useful for API states and discriminated unions.
  • Literal types provide better control over allowed values.

🧠 Quick Quiz

Question: Which of the following is a valid TypeScript literal type?