Exploring a function that reverses a pair, I am seeking to define aliases for both the input and output in a way that can be utilized within the function itself and its type signature.
function reverse<A, B>([a, b]: [A, B]): [B, A] {
return [b, a];
}
I am interested in passing input and output types as arguments to 'reverse,' making it more intuitive for the caller compared to using A
and B
which are more internal details.
function reverse<I extends [infer A, infer B], O implements [B, A]>([a, b]: I): O {
return [b, a];
}
In an attempt to experiment with this concept, I made the following modifications. However, it does not work as intended since the output is not guaranteed to extend [B, A]
.
function reverse<A, B, I extends [A, B], O extends [B, A]>([a, b]: I): O {
return [b, a];
}
Are there any alternative solutions to make this approach successful?>