What do extends keyof and in keyof mean in TypeScript?

In TypeScript, what do “extends keyof” and “in keyof” mean?

In TypeScript, I’ve come across some types defined using extends keyof or in keyof, but I’m having trouble fully understanding what they mean. Here’s what I think I know:

When you use keyof, it returns a union type of all the property names that exist on a given type. For example, if I have:

type T = keyof string;

Then T would be equivalent to startsWith | endsWith | trim | substring | ..., which are all methods of the string type. Is this understanding correct?

Now, when I encounter extends keyof or in keyof, I try to reason through their meanings. Here’s my thinking:

  • extends keyof seems like any type that derives from T, meaning it includes all of these possible values, but maybe it also has more.
  • in keyof seems like any type that takes values from T, but it doesn’t necessarily have to include all of them—some values could be omitted.

So from this perspective, I think extends keyof might represent a “greater than or equal to” relationship, while in keyof might represent a “less than or equal to” relationship. Is this correct? Or is there a different, more accurate explanation for how extends keyof and in keyof work in TypeScript?

Hello keerti_gautam!

Understanding TypeScript’s extends keyof can be a bit challenging at first, but it’s a powerful feature once you get the hang of it! Here’s a breakdown to help clarify how it works.

In TypeScript, extends keyof is often used to create constraints on types by ensuring that a specific type matches one of the property names (or “keys”) of another type. When we write T extends keyof U, we’re saying that T must be one of the property names within the type U. This means T is limited to only those values that are valid property names within U, giving us more control over what values are allowed.

For example, imagine you have an object type U with properties like name and age. By using T extends keyof U, you ensure that T can only be one of these property names. This is incredibly useful for maintaining type safety, as it prevents you from assigning values to T that aren’t part of U.

In contrast, in keyof is used in different constructs, allowing you to iterate over the keys of an object type. Together, these features enable developers to build more flexible and safe code structures in TypeScript.

Hope this clears things up! Feel free to ask further questions or share examples where you’ve found extends keyof useful.

Thank you

Hello TypeScript Enthusiasts,

Let’s dive into a key concept in TypeScript that often piques the curiosity of many developers: the in keyof construct. This powerful tool in TypeScript is frequently used in mapped types to iterate over the keys of an object type, making it essential for dynamic type manipulation.

When we say T in keyof U, we’re creating a mapped type where T represents each key in U. Think of it as generating a new type by referencing all the keys in another type. This technique is not limited to a specific key; instead, it allows us to build types that map over the properties of another type and apply specific transformations to them.

In practice, in keyof enables you to write more flexible and reusable code by building types that adapt based on the structure of existing types. This can be particularly handy when working on complex TypeScript applications that require dynamic typing capabilities.

Hope this helps you better understand the in keyof concept! Feel free to ask any questions, share your thoughts, or add your own examples below.

Best regards

Hello everyone! :blush:

If you’re diving into TypeScript, you’ve probably encountered the terms extends keyof and in keyof. These two concepts are related but serve unique purposes, each helping developers work with types in powerful ways.

Let’s break down the difference:

  • extends keyof is used to constrain a type to one of the keys of a specified object. Think of it as a way to ensure that a type fits within a certain set of options, making it a more restrictive operation. It’s like saying, “This type can only be one of these specific keys.”

  • in keyof, on the other hand, is used to iterate over the keys of an object. It allows you to create new types or transform existing ones by looping through the keys. This gives you a lot of flexibility to dynamically create types based on an object’s structure.

In short, while both focus on working with keys of an object, extends keyof is about setting type constraints, and in keyof is about iterating over those keys to shape new types. So, you can think of extends keyof as a “restriction,” while in keyof is a tool for “creation” and dynamic type transformations.

I hope this clarifies the distinction between these two TypeScript utilities! Let me know if you have more questions or need examples.

Best regards