1

I'm working on wrapping a function in a customized one in TypeScript. For that, I want to correctly pass down the types.

I looked at similar questions like this one, but they all seem to answer the question on how to extract from a class but not a function.

I think en example is the easiest explanation:

// we have a given function like this:
function genericFunction<T extends object>(a: T) {
  return a;
}

// we want to extract the T type to pass it down for smth like this:

type ExtractedT = typeof genericFunction<IWANTTHEGENERICTYPE>; // (THIS IS WHERE I DON'T KNOW HOW TO PROCEED);

function myGenericFunction<T extends ExtractedT>(a: T, b: string) {
   console.log(b, "- is a newly introduced variable.")
   genericFunction<T>(a);
}

Of course, the example is a highly abstracted example, but my goal should hopefully be clear.

I appreciate your answers!

EDIT:

I updated the example since it didn't really make sense before. My goal is to "dynamically" wrap a method from a framework i.e. (my sophisticated use case also extracts the Parameters<> types, but that's smth else.)

So I guess my goal is to have a way of detecting the generic types of a function and copying them over to my own function. But I start to think this that this blows the scope of TypeScript and just hovering over the generic types and importing / copy-pasting them for the wrapper function might be the better solution

josias
  • 1,326
  • 1
  • 12
  • 39
  • `typeof genericFunction` will just return `function`. I don't think you can get the "type" of a generic function without actually calling it at runtime (even then you can't actually get the [type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)). –  Aug 11 '20 at 09:25
  • 1
    I'm not sure I understand the example. You cannot have `ExtractedT` before you call `genericFunction` since it's the call that defines what `T` would be. You can always explicitly pass the generic argument, say, by using `genericFunction(someArg)` but if you're explicit you *would* know what `T` would be. I really don't see what `myGenericFunction` should actually be doing with `ExtractedT` instead of just being generic itself: `function myGenericFunction(a: T, b: string) { genericFunction(a) }` – VLAZ Aug 11 '20 at 09:35
  • @VLAZ you are completely right, it doesn't make sense like this. And you are also right, that if I know it was T, I could use a new T, **but** that's exactly the problem, because it's not just T but it extends some other types, therefore creating a new T would cause an error – josias Aug 11 '20 at 09:58
  • In that case you probably need a more representative example of the problem. Right now, I cannot understand what the exact issue is and thus I cannot propose an adequate solution. I'd rather not guess. – VLAZ Aug 11 '20 at 10:04
  • @VLAZ your are right. I will update it later, I currently have another problem which I need to understand first – josias Aug 11 '20 at 11:00
  • Unsure I understand everything, but what you're trying to do may be impossible. Take a look at this : https://stackoverflow.com/questions/47312116/how-to-get-generic-classt-name-of-typescript – Kewin Dousse Aug 11 '20 at 11:48
  • @VLAZ I updated the example. I basically wan't the whole generic type (the one you see when you hover over types in VSCode i.e) to be dynamically in sync w/ the wrapper function instead of having to import al the types they extend, etc. but doing that manually might be the way to go – josias Aug 11 '20 at 15:31
  • I think some clues are here : https://stackoverflow.com/questions/52963637/typescript-is-it-possible-to-get-the-return-type-of-a-generic-function – Adrien Clerc Aug 19 '21 at 15:24

0 Answers0