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