1

In typescript is there a way to reuse parameter types as follows:

interface Box {
    create: (paramToReuse: (value: any) => void) => void
}
// is there a way to reference paramToResuse as a type? e.g.
let x: Box['create']['paramToReuse']

This can be done the other way around: by first defining paramToReuse and then referencing it in Box's interface, but can it be done the way I've shown above?

1 Answer 1

5

If it's acceptable to reference it by its parameter index (0) instead of its name ("paramToUse"), that can be done with the helper type Parameters, which takes a function and turns it into a tuple of its parameters:

interface Box {
    create: (paramToReuse: (value: any) => void) => void
}

let x: Parameters<Box["create"]>[0] // x is of type (value: any) => void

Playground link

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

1 Comment

Awesome - that works - thank you!

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.