I have a set of objects, each with its own properties:
const a = { a1 : 1, a2 : 2 } as const
const b = { b1 : `1`, b2 : `2` } as const
The function f
takes all these objects as a typed tuple:
function f<
T extends { [key : string] : any }[]
> (
...t : [...{ [i in keyof T] : T[i] }]
) {
// todo
}
f(a, b)
The goal is to return any property of any of these objects.
In this case, the expected result should be 1 | 2 | "1" | "2"
.
The problem is that I can't figure out how to properly describe the return type.
I have tried T[number][keyof T[number]]
but it have failed, probably due to possible differences in indexes for T
and keyof T
.
Then I wrote a wrapper for it:
type PropertyOf<T extends { [key : string] : any }> = T[keyof T]
And specify the return type of f
as PropertyOf<T[number]>
. But it still doesn't work.
Despite PropertyOf
returns expected 1 | 2
for PropertyOf<{ a1 : 1, a2 : 2 }>
, when used as PropertyOf<T[number]>
in f
the function return type is never
.
What is the reason and how to fix this? Thanks.