In TypeScript, you can achieve unique generic types for each entry in an object by using mapped types. To do this, you can use the keyof and extends keywords to iterate over the keys of the object and create a new type for each entry. Here's an example of how you can accomplish this:

type ObjectWithUniqueTypes<T> = { [K in keyof T]: T[K] extends number ? number : string; }; // Example usage interface ExampleObject { age: number; name: string; isActive: boolean; } type UniqueTypesForObject = ObjectWithUniqueTypes<ExampleObject>; // The resulting type will be: // type UniqueTypesForObject = { // age: number; // name: string; // isActive: string; // }

In this example, the ObjectWithUniqueTypes mapped type iterates over each key (K) in the input type T, and for each entry, it checks whether the value of the property extends (is a subtype of) number. If it does, the resulting type for that entry is number; otherwise, it's string.

You can customize the logic inside the mapped type based on your specific requirements. This approach allows you to create unique types for each entry in an object based on its specific value types.

Have questions or queries?
Get in Touch