0

I am passing a callback function as a function parameter, and I need to know the type of this parameter. I don't want to define the type manually as it's too complicated. VSCode can infer the parameter type, but how can I get a type alias?

Is there some methods like ReturnType and InstanceType that can do the job?

enter image description here

1 Answer 1

3

This should correctly infer the type of response:

type AddProfileResponseType = 
  typeof onAddProfileDone extends (cb: (r: infer R) => any) => any 
    ? R 
    : never

If the library defines a type for the onAddProfileDone function. You should use it instead of typeof onAddProfileDone.

Playground


Or even simpler:

type AddProfileResponseType = Parameters<Parameters<typeof onAddProfileDone>[0]>[0]

Playground

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks the second one works for me! Didn't try the first version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.