我是打字稿新手,将现有的js代码迁移到ts代码。

显然connect-session-firebase没有自定义类型,因此我决定创建一个。

这就是我在代码中使用它的方式

import FirebaseSession from 'connect-session-firebase'

app.use(
            session({
                store: new FirebaseSession({
                    database: context.realtimeDB
                }),
                name: '__session', // Must use this name for firebase hosting
                secret: 'kkkkj9h9', // TODO: Move to ENV
                resave: true,
                saveUninitialized: true
            })
        )


这是我在哪里打字

declare module "connect-session-firebase" {
  import * as admin from 'firebase-admin'
  import { Request, Response, NextFunction } from "express";

  interface firebaseSession {
    database: admin.database.Database | admin.firestore.Firestore,
    sessions?: string,
    reapInterva?: string,
    errorIfSessionNotFound?: any
  }
}


而且我知道这是错误的,因为在这里store: new firebaseSession({我遇到以下错误


this expression is not constructable.
  Type 'typeof import("connect-session-firebase")' has no construct signatures.



有人可以告诉我我做错了什么以及如何解决?

更新:我将firebaseSession更新为FirebaseSession并尝试使用new(),但还是没有运气

declare module "connect-session-firebase" {
  import * as admin from 'firebase-admin'
  import { Request, Response, NextFunction } from "express";

   interface FirebaseSession {
    new(
    database: admin.database.Database | admin.firestore.Firestore,
    sessions?: string,
    reapInterva?: string,
    errorIfSessionNotFound?: any)
  }
}


但同样的错误

更新:这是我更新的代码:

import FirebaseSession from 'connect-session-firebase'

app.use(
            session({
                store: new FirebaseSession({
                    database: context.realtimeDB
                }),
                name: '__session', // Must use this name for firebase hosting
                secret: 'kkkkj9h9', // TODO: Move to ENV
                resave: true,
                saveUninitialized: true
            })
        )


类型为connect-session-firebase的be

import * as admin from 'firebase-admin'

declare class FirebaseSession {
  constructor(options: FirebaseSession.Options)
}


declare namespace FirebaseSession {
  export interface Options {
    database: admin.database.Database | admin.firestore.Firestore,
    sessions?: string,
    reapInterva?: string,
    errorIfSessionNotFound?: any
  }
}


export = FirebaseSession


请注意,虽然这不会在编译时引发错误,但会创建类似的JS代码,例如

const connect_session_firebase_1 = __importDefault(require("connect-session-firebase"));
    app.use(express_session_1.default({
            store: new connect_session_firebase_1.default({
                database: context.realtimeDB
            }),
            name: '__session',
            secret: 'kkkkj9h9',
            resave: true,
            saveUninitialized: true
        }));


引发错误connect_session_firebase_1.FirebaseSession is not a constructor


无法读取未定义的属性原型。

最佳答案

你可以这样尝试吗?

1)使用创建一个type.d.ts


declare module "connect-session-firebase" {
    import * as admin from 'firebase-admin'
    export interface OptionsShape {
        database: admin.database.Database | admin.firestore.Firestore,
        sessions?: string,
        reapInterva?: string,
        errorIfSessionNotFound?: any
    }

    export interface FirebaseSession {
       new(n: OptionsShape): void
    }

    export class FirebaseSession {
       constructor(n: OptionsShape);
    }

    export default FirebaseSession;
}


2)导入您的模块并使用它(应该输入它,不要抱怨输入任何

import FirebaseSession from 'connect-session-firebase'

var firebase = new FirebaseSession({
 //params
});


编辑:正如我在评论中所说,您可以像这样修复编译,但是现在由您来确定库的正确类型了。我不知道该库,所以我不知道new FirebaseSession({})的返回值是什么,或者FirebaseSession作为属性和方法具有什么...

您必须阅读该程序包并使其适应types.d.ts,这样它才能正常工作。您可能可以从软件包创建者那里获得帮助,并在完成后输入@types/connect-session-firebase。但是,如果只是为了使编译器正常工作,则可以继续打补丁(但不建议这样做)

declare module "connect-session-firebase" {
    import * as admin from 'firebase-admin'
    import { Store, MemoryStore } from 'express-session';
    export interface OptionsShape {
        database?: admin.database.Database | admin.firestore.Firestore,
        sessions?: string,
        reapInterva?: string,
        errorIfSessionNotFound?: any
    }

    export interface FirebaseSession {
       new(n: OptionsShape)
    }

    export class FirebaseSession extends MemoryStore {
       constructor(n: OptionsShape);
    }

    export default FirebaseSession;
}

10-08 07:39