I'm working on a repo that's all in javascript but that exports handwritten type declarations (automerge/index.d.ts).
The structure of the codebase is that it has a Frontend and a Backend, plus a public API that offers some convenience functions of its own, in addition to re-exporting some functions directly from the Frontend and the Backend.
Something like this:
declare module `foo` {
// functions that only exist in the public API
function a
function b
function c
// functions exposed directly from namespace A
function q
function r
function s
// functions exposed directly from namespace B
function x
function y
function z
namespace A {
function q
function r
function s
function t
}
namespace B {
function v
function w
function x
function y
function z
}
}
Here's an excerpt from the actual code showing how we're currently writing duplicate declarations for the re-exported functions.
declare module 'automerge' {
...
function getObjectById<T>(doc: Doc<T>, objectId: OpId): Doc<T>
namespace Frontend {
...
function getObjectById<T>(doc: Doc<T>, objectId: OpId): Doc<T>
}
...
}
Is there a way to avoid writing these declarations twice?
One possibility would be to define an arrow function type alias and use that in both places. E.g.:
Unfortunately it is not possible to do the same directly with "regular" function declarations (see here).
Arrow functions and function declarations are not exactly the same, especially around scoping of
this
within the function. For example arrow functions cannot have athis
parameter:But if you are not relying on any features they differ on, or can just switch to arrow functions in your js code to be safe, then this should work.