satisfies Keyword in Typescript

Do you want to know about the Typescript satisfies keyword? In this Typescript tutorial, I will explain how to use the satisfies Keyword in Typescript; Typescript satisfies Keyword syntax. Also, I will show you a few examples of satisfies Keyword in Typescript.

To use the TypeScript satisfies keyword, declare an object and then use the syntax object satisfies Type to check if the object conforms to a specific type. This keyword allows for type checking without altering the object’s inferred type, ensuring that the object meets the minimum requirements of the specified type. For example, myObject satisfies MyInterface checks if myObject meets the criteria defined in MyInterface.

What is the satisfies Keyword in Typescript?

The satisfies keyword in TypeScript is used for type checking. It enables developers to verify if a given object conforms to a specific type without changing the object’s inferred type. This is particularly useful when you want to ensure type safety without modifying the existing type inference of an object.

satisfies Keyword in Typescript examples

Now, let us see a few examples of how to use the satisfies Keyword in Typescript.

Example 1: Basic Usage of satisfies

Let’s start with a simple example to understand the basic usage of satisfies in Typescript.

interface Person {
    name: string;
    age: number;
}

const john = {
    name: "John Doe",
    age: 30,
    occupation: "Engineer"
};


john satisfies Person;

In this example, we have an interface Person and an object john. By using john satisfies Person, we check if john meets the requirements of the Person interface. Note that john has an additional property occupation, which is not in Person, but the code will still compile successfully because john satisfies the minimum requirements of Person.

You can see the output in the screenshot below, I executed the code in the VS code.

satisfies keyword in Typescript

Example 2: Ensuring Type Safety

The satisfies keyword in Typescript becomes very useful when ensuring that a complex object meets a certain type specification.

interface Book {
    title: string;
    author: string;
    publishedYear: number;
}

const myBook = {
    title: "Typescript Magic",
    author: "Jane Smith",
    publishedYear: 2023,
    genre: "Programming"
};

// Ensuring 'myBook' conforms to 'Book' interface
myBook satisfies Book;

Here, myBook includes an extra genre field. By using satisfies, we ensure that myBook has at least the fields required by the Book interface.

Example 3: Combining satisfies with Functions

The Typescript satisfies keyword can also be used in the context of functions to ensure the arguments meet certain type criteria.

interface Employee {
    id: number;
    name: string;
    department: string;
}

function createEmployee(employee: unknown) {
    employee satisfies Employee; // Type checking
    // Additional logic to create an employee
}

const newEmployee = {
    id: 1,
    name: "Alice",
    department: "HR",
    joiningDate: "2023-01-01"
};

createEmployee(newEmployee);

In this function, createEmployee, we use employee satisfies Employee to ensure the passed argument adheres to the Employee interface.

Conclusion

The satisfies keyword in TypeScript ensures type safety in a flexible and non-intrusive way. In this Typescript tutorial, I have explained how to use the satisfies keyword in Typescript and its syntax. I have also explained how to use the Typescriptsatisfies keyword with a few examples.

You may also like:

>