Skip to content

Instantly share code, notes, and snippets.

@selcukguler0
Created March 31, 2023 09:36
Show Gist options
  • Save selcukguler0/06b14f5d55b1e691dc1cc4e0d921351e to your computer and use it in GitHub Desktop.
Save selcukguler0/06b14f5d55b1e691dc1cc4e0d921351e to your computer and use it in GitHub Desktop.
Understanding TypeScript's `satisfies` Operator

Understanding TypeScript's satisfies Operator

TypeScript is a powerful tool for developing large-scale JavaScript applications. One of its key features is the ability to define complex types and interfaces that can be used to enforce type safety throughout your codebase. One lesser-known feature of TypeScript is the satisfies operator, which allows you to check whether an object conforms to a particular type or interface at runtime. In this article, we'll explore how the satisfies operator works, and how you can use it in your own TypeScript projects.

What is the satisfies Operator?

The satisfies operator is a TypeScript feature that allows you to check whether an object conforms to a particular type or interface. It works by taking two arguments: the first is a type or interface, and the second is an object to be checked against that type or interface. Here's an example:

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

const person: unknown = {
  name: 'Alice',
  age: 30,
  occupation: 'Software Engineer',
};

const isPerson = (obj: any): obj is Person => {
  return 'name' in obj && typeof obj.name === 'string' && 'age' in obj && typeof obj.age === 'number';
};

console.log(isPerson(person)); // false

In this example, we define a Person interface that specifies that an object must have a name property of type string, and an age property of type number. We then create an object called person that has these properties, as well as an additional occupation property. We want to check whether person satisfies the Person interface, so we create a function called isPerson that takes an object as its argument and returns a boolean indicating whether the object satisfies the interface.

The isPerson function uses the in operator to check whether the name and age properties are present in the object, and the typeof operator to check that they have the correct types. If both checks pass, the function returns true, indicating that the object satisfies the Person interface. If either check fails, the function returns false.

We then call the isPerson function with the person object, and log the result to the console. In this case, the function returns false, because the person object has an additional occupation property that is not present in the Person interface.

Why Use the satisfies Operator?

You might be wondering why you would want to use the satisfies operator in your TypeScript code. After all, TypeScript is already a statically typed language, so shouldn't the compiler catch any type errors at compile time?

While it's true that TypeScript provides powerful static type checking, there are still cases where you might want to perform runtime checks on objects to ensure that they meet certain requirements. For example, you might be working with data that is coming from an external source, such as an API or a database, and you want to ensure that the data is in the expected format before you use it in your code. Or you might be working with dynamic user input, and you want to ensure that the input conforms to a particular schema before you process it.

In these cases, the satisfies operator can be a powerful tool for enforcing type safety at runtime. By defining interfaces that describe the expected shape of objects, and using the satisfies operator to check that objects conform to those interfaces, you can catch type errors before they cause problems in your code.

Conclusion

In this article, we've explored the satisfies operator in TypeScript, and how it can be used to check whether an object conforms to a particular type or interface at runtime. While TypeScript already provides powerful static type checking, the satisfies operator can be a useful tool for enforcing type safety at runtime, especially when working with dynamic data sources or user input.

To use the satisfies operator in your own TypeScript code, you'll need to define interfaces that describe the shape of objects you expect to work with, and then create functions that use the in and typeof operators to check that objects conform to those interfaces. With these tools at your disposal, you can build more robust and reliable TypeScript applications that catch type errors before they cause problems at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment