I've come across a challenge when trying to extend the interfaces of third-party libraries globally. Whenever I import other files at the root level, the declaration file loses its global nature.
Let me illustrate this with an example:
Suppose I want to add a property called "user" of type "MyUser" to Express's Request object. If "MyUser" was a string, I could simply do the following:
declare namespace Express {
interface Request {
user: string;
}
}
And then use it somewhere else in the code like this:
...
function(..., req: Request, ...) {
...
req.user; // it's a string
...
}
However, the following approach doesn't work:
import MyUser from "../../src/types/MyUser";
declare namespace Express {
interface Request {
user: MyUser;
}
}
This is because of the top-level import. Is there a way to work around this issue? It's frustrating that the ability to merge declarations is restricted when it comes to using custom types within them.