'function declaration for a generic function return wrapper

How to declare the function wraps that takes any function and returns a version of it that differs only in returning an object that wraps the original return type in its prop value
... and support generics
try it here

type Fn = (...args: any) => any

declare function wraps<F extends Fn>(): (...args: Parameters<F>) => { value: ReturnType<F> }

type AFunction = <T>(t: T)=> { a: T }


const wf = wraps<AFunction>()
// const wf: (t: unknown) => { value: { a: unknown } }

const y = wf(100)
// got:
// const y: { value: { a: unknown } }
//
// expected: 
// const y: { value: { a: number } }

// as for:
declare const aFunction:AFunction
const x = aFunction(100)
// const x: { a: number }

to better explain the subtlety of the question, here's another way of presenting it, more similar to my real use case :

type FnMap={
  fn1: <T>(t: T) => { a: T }
  fn2: <T>(t: T) => { b: T }
} 

declare function wraps<N extends keyof FnMap>(name:N): (...args: Parameters<FnMap[N]>) => { value: ReturnType<FnMap[N]> }


const wf1 = wraps('fn1')
// const wf1: (t: unknown) => { value: { a: unknown } }

const wf1Res = wf1(100)
// got:
// const wf1Res: { value: { a: unknown } }
//
// expected: 
// const wf1Res: { value: { a: number } }

const wf2 = wraps('fn2')
// const wf2: (t: unknown) => { value: { b: unknown } }

const wf2Res = wf2('str')
// got:
// const wf2Res: { value: { b: unknown } }
//
// expected: 
// const wf2Res: { value: { b: string } }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source