1

I am in need of extracting a type from an object literal that I am constructing by hand. I have the following:

const makeFunc: { key: string, val: (p: TParam) => TResult } = <TParam, TResult>(p: TParam) => doSomething(p);

const myObj = {
    getSomething: makeFunc(a),
    getSomethingElse: makeFunc(b)
}

Let's assume a is of type A and makeFunc(a) is of type AR and similarly b is of type B and makeFunc(b) is of type BR. Now I need a type that resembles something like this:

interface extractedType {
    getSomething: (a: A) => AR,
    getSomethingElse: (b: B) => BR
}

Is there a way to accomplish this? If so, could someone throw some light or point me in the right direction?

Charles Prakash Dasari
  • 4,964
  • 1
  • 27
  • 46

1 Answers1

0

I don't currently have access to a computer, so I can't verify this, but something like this should work,

interface extractedType { 
        getSomething: <A, AR> (a: A) => AR, 
        getSomethingElse: <B, BR> (b: B) => BR 
    }

you might have to play around with it a bit, hopefully it helps!

Source

https://www.typescriptlang.org/docs/handbook/generics.html


Edit: Link to thread that might be related, see comments.

https://stackoverflow.com/a/44078574/7307141

JRasmusBm
  • 46
  • 6
  • What I needed to know was how I can extract this interface type using a piece of code. I know what the resultant interface type would be, but given an object literal, I need a function that would extract the resultant interface type I showed. – Charles Prakash Dasari Jan 27 '18 at 10:44
  • Hmm... Okay, that is more difficult, and I don't have a good answer for that, I post a link to a thread that might be related, good luck! – JRasmusBm Jan 28 '18 at 06:40