Consider a scenario where you have an object with functions
{
fnNumber: (x: number) => x,
fnString: (x: string) => x
}
and you want to transform it into a type like this
{
fnNumber: number,
fnString: string
}
This transformation can be achieved using the following code snippet
type FnParam<T> = {[key in keyof T ]: T[key]}
type FnObject<T> = {[key in keyof T]: (x: T[key]) => any}
export function create<T>(x: FnObject<T>): FnParam<T> {
return {} as any
}
const types = create({
fnNumber: (x: number) => x,
fnString: (x: string) => x
})
types: : {fnNumber: number, fnString: string}
However, if the functions have more than one parameter
{
fnNumber: (x: number, y: string) => x,
fnString: (x: string) => x
}
You would need to represent them as tuples like this
{
fnNumber: [number, string],
fnString: [string]
}
To handle this case, you can use the following approach
type FnParam2<T> = {[key in keyof T ]: T[key]}
type FnObject2<T> = {[key in keyof T]: (...x: T[key]) => any}
export function create2<T>(x: FnObject2<T>): FnParam2<T> {
return {} as any
}
However, TypeScript may struggle to infer tuple types from ...x: T[key]
Is there a way to resolve this issue?