როგორ შემიძლია გამოვასწორო ეს TypeScript პრობლემა Koa Session-სთან Koa აპლიკაციის გამოყენებით, რომელიც გენერიკულად არის ტიპიზირებული?


მე ვიყენებ შემდეგ ინტერფეისებს ჩემი კონტექსტისა და მდგომარეობის განსაზღვრისთვის koa-ში:

export interface AppState extends Koa.DefaultState {
  //...
}
export interface AppContext extends Koa.DefaultContext {
  models: DomainModels;
  connection: Connection;
}

და შემდეგ დააინსტანცირე koa ასე:

const app = new Koa<AppState, AppContext>();
app.use(session(sessionConfig, app));

პრობლემა ის არის, რომ koa-session-ის ტიპები იყენებენ დეკლარაციის გაერთიანებას და იწვევენ შემდეგ შეცდომას:

Argument of type 'import("/Users/chance/Projects/pear/packages/api/node_modules/@types/koa/index.d.ts")<AppState, AppContext>' is not assignable to parameter of type 'import("/Users/chance/Projects/pear/packages/api/node_modules/@types/koa/index.d.ts")<DefaultState, DefaultContext>'.
  Types of property 'middleware' are incompatible.
    Type 'Middleware<ParameterizedContext<AppState, AppContext>>[]' is not assignable to type 'Middleware<ParameterizedContext<DefaultState, DefaultContext>>[]'.
      Type 'Middleware<ParameterizedContext<AppState, AppContext>>' is not assignable to type 'Middleware<ParameterizedContext<DefaultState, DefaultContext>>'.
        Type 'ParameterizedContext<DefaultState, DefaultContext>' is not assignable to type 'ParameterizedContext<AppState, AppContext>'.
          Type 'ExtendableContext & { state: DefaultState; } & DefaultContext' is missing the following properties from type 'AppContext': models, connection, session, sessionOptionsts(2345)

მიმართვადი ნაწილები @types/koa-session-დან:

declare function session(CONFIG: Partial<session.opts>, app: Koa): Koa.Middleware;

declare function session(app: Koa): Koa.Middleware

declare module "koa" {
    interface Context {
        session: session.Session | null;
        readonly sessionOptions: session.opts | undefined;
    }
}