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?
CodePudding user response:
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