Trying to implement a TypeScript function that takes a single-argument function and returns a modified version of it with the argument wrapped in an object. However, struggling to keep track of the original function's generics:
// ts v4.5.5
type FnType<Arg, Ret> = (arg: Arg) => Ret
type Wrapped<VFn extends FnType<any, any>> = (wrap: {
_: VFn extends FnType<infer A, any> ? A : never
}) => VFn extends FnType<any, infer B> ? B : never
// Created a function that takes a `FnT extends FnType` and produces Wrapped<FnT>
type Wrapper = <FnT extends FnType<any, any>>(v: FnT) => Wrapped<FnT>
declare const wrapper: Wrapper
// The resulting `wrapped` function loses track of generic type T
const wrapped1 = wrapper(<T>(t: T) => ({ t })) // const wrapped1: Wrapped<(<T>(t: T) => { t: T; })>
const ret1 = wrapped1({ _: { a: 1 } }) // const ret1: { t: unknown; }
type MyFnType = <T>(t: T) => { t: T }
const myFnType:MyFnType = <T>(t: T) => ({ t })
// Even explicitly defining and passing FnType as generic and argument !
const wrapped2 = wrapper<MyFnType>(<T>(t: T) => ({ t })) // const wrapped2: Wrapped<MyFnType>
const ret2 = wrapped2({ _: { a: 1 } }) // const ret2: { t: unknown; }
const wrapped3 = wrapper<MyFnType>(myFnType) // const wrapped3: Wrapped<MyFnType>
const ret3 = wrapped2({ _: { a: 1 } }) // const ret3: { t: unknown; }