I'm facing an issue where I have an Array of generic objects and want to iterate over them, but TypeScript is not allowing me to do so. Below is a snippet of the code I am working with. Any ideas on how to solve this problem?
type someGeneric<T> = { item: T };
type stringGeneric = someGeneric<string>;
type numberGeneric = someGeneric<number>;
type someFunction = <T>(generic: someGeneric<T>) => T;
const someFunction: someFunction = (generic) => generic.item;
const stringGeneric: stringGeneric = { item: 'some String' },
numberGeneric: numberGeneric = { item: 12 };
let genericArray = [stringGeneric, numberGeneric];
genericArray.forEach(generic => {
someFunction(generic); // Error On This line.
});
If you'd like to test out the code yourself, feel free to paste it into this online tool. Unfortunately, I can't share the code directly here.