I have types that enforce relationship between both members of a tuple within array of these tuples when passed as argument to function i.e. second member is a function that takes in a data parameter matching first member.
First checking whether Tuple relationships hold:
export type TupleDataFn<Type>
= Type extends [infer Data, infer Fn]
? Fn extends ((data: Data) => any)
? Type
: never
: never
A mapped type that applies the check for all members of array:
export type ArrayOfTuples<A>
= {[K in keyof A]: TupleDataFn<A[K]>}
A test function
export const test = <H extends [any, any][]>(handler: ArrayOfTuples<H>) => {}
And finally the usage – here the parameter of function without
test([
// I can do this explicitly and this works as expected
['explicit', (data: string) => {}],
// in remaining cases, the data is 'any'
// instead of infered types matching the first member
['text', (data) => {}],
[42, (data: string) => {}],
[{ count: 10, data: "test"}, (data) => {}]
])
Here the data parameter is always ‘any’ unless explicitly defined (in that case type checks work and enforce type of function parameter as expected).
Is there any way to achieve the expected behavior, where type of data parameter in function tuple member is inferred from type of first member?