I am currently utilizing Koa () as the framework for my backend, which consists of Node.js + TypeScript.
Koa permits and recommends using the built-in `ctx.state` to store and pass data between different middleware functions. I have been adhering to this practice and it has been functioning seamlessly.
However, I am facing a challenge in implementing TypeScript with `ctx.state`, as it gets populated at runtime and I am unsure where to begin. I intend to structure `ctx.state`, particularly for the backend response, based on this interface (with various extensions):
export interface ResObj {
auth: any;
data: any;
dataTot?: number;
error: {
stack: string;
code: number;
message: string;
};
extra: any;
message: string;
status: number;
tech: {
needRefresh: boolean;
};
}
While one approach could be importing this interface into each endpoint and employing some form of type assertion, that is precisely what I aim to avoid. I have scoured through the Koa documentation and Stack Overflow but have not come across any guidance on this matter.