Avatar ·

Error TS2322: "could be instantiated with a different subtype of constraint 'object'" — how to fix?

Hi! I'm new to TypeScript and encountered a strange error when using generics. I have a simple function that should take an object and return its keys. I want the function to work with any object, but preserve the return type as an array of keys of that specific object. Here's my code: ```typescript function getKeys(obj: T): (keyof T)[] { return Object.keys(obj) as (keyof T)[]; } function processObject(data: T) { // Here I want to pass data to another function const handler = (input: U): void => { const keys = getKeys(input); console.log(keys); }; handler(data); // ERROR here! } ``` TypeScript gives an error: `Argument of type 'T' is not assignable to parameter of type 'U'. 'U' could be instantiated with a different subtype of constraint 'object'.` I don't understand why this happens. Both T and U are constrained to type `object`. Why does TypeScript