TypeScript: Inferred return type of function not matching expected type in a generic function
I've been struggling with this for a few days now and could really use some help... I'm facing an issue with a generic function in TypeScript where the inferred return type doesn't seem to match the expected type. I'm using TypeScript 4.5 and I have a generic function that is supposed to return an array of objects where each object contains a `name` and an `age`. Hereβs the function Iβve written: ```typescript interface Person<T> { name: string; age: number; extraData?: T; } function createPersons<T>(data: T[]): Person<T>[] { return data.map(item => ({ name: 'Person', age: 30, extraData: item })); } ``` When I call this function like so: ```typescript const result = createPersons([{ hobbies: ['sports'] }, { hobbies: ['music'] }]); ``` I expect `result` to have the type `Person<{ hobbies: string[] }>[]`, but TypeScript infers it as `Person<{}>[]`. It seems like TypeScript is not inferring the type of `T` correctly from the input array. Iβve also tried explicitly typing the function call with: ```typescript const typedResult = createPersons<{ hobbies: string[] }>([{ hobbies: ['sports'] }, { hobbies: ['music'] }]); ``` However, even with the explicit type, the inferred return type still seems to ignore the type definition of `T`. The expected type for `typedResult` should be `Person<{ hobbies: string[] }>[]` but I get `Person<{}>[]` instead. I've checked my TypeScript config, and I have `strict` mode enabled, as well as `noImplicitAny`. Does anyone have any insights on why this might be happening, or is there a way to ensure the return type matches the expected structure? Any help would be greatly appreciated! My development environment is macOS. How would you solve this? Thanks for any help you can provide!