Conceptually, transaction only rethrows the error produced by the closure, so it seems reasonable that the compiler could preserve the concrete error type (DatabaseError) here.
However, the code currently fails to compile.
Is this a known limitation of the current typed throws implementation, or is there a deeper design reason why rethrowscannot preserve the concrete error type?
Additionally, what would currently be considered the best approach to work around this limitation while still preserving typed errors as much as possible?
There is no longer any reason to use rethrows now that typed throws is available. You can declare a generic function that throws a type parameter type instead. Since Never conforms to Error, substituting Never for your type parameter gives you the non-throwing behavior.
Similarly, if you are relying on being able to throw a different error if-and-only-if the callback throws, typed throws doesn't quite mean the same thing.
I don't believe the error-type-as-extra-arg pattern allows you to pass no arguments, since the compiler will complain that the error type could be inferred from either argument.
More overloads does fix it, but the particular case I came across this in I would've had to duplicate dozens of functions to achieve it. It didn't feel like a worthwhile tradeoff.