Node.js 创建构造函数签名

Node.js 创建构造函数签名,node.js,typescript,Node.js,Typescript,我是typescript新手,正在将现有的js代码迁移到ts代码 显然,连接会话firebase没有自定义键入,所以我决定创建一个 这就是我在代码中使用它的方式 import FirebaseSession from 'connect-session-firebase' app.use( session({ store: new FirebaseSession({ database: context

我是typescript新手,正在将现有的js代码迁移到ts代码

显然,
连接会话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:newfirebasession({
我得到了以下错误

有人能告诉我我做错了什么,我怎样才能纠正它吗

更新:我将
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)
  }
}
但同样的错误

更新:这是我的更新代码: 使用类型
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\u session\u firebase\u 1。FirebaseSession不是构造函数

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


要将接口标记为可构造的,请为其提供
new(…)
“方法”(更多):


如果可能的话,我还强烈建议对任何可构造类型进行初始封顶(
FirebaseSession
,而不是
FirebaseSession
)。

您可以这样尝试吗

1) 使用创建一个类型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会话firebase
。但是如果只是为了让编译器工作,您可以继续修补(但不推荐)


我不确定是否正确,但仍然是相同的错误。更新了question@anny123-请注意,您的更新没有为
new
提供返回值。您应该得到一个错误。如果您按照答案中所示的操作,会发生什么情况?@anny123-更新后的问题也不一致。您使用的是
firebase吗Session
FirebaseSession
?抱歉,我的错。添加了两个屏幕截图供您参考此先检查我的答案:
类型为“{store:FirebaseSession;name:string;secret:string;ResSave:true;saveUninitialized:true;”的参数'不可分配给'SessionOptions'类型的参数。属性'store'的类型不兼容。类型'FirebaseSession'不可分配给'store | MemoryStore | undefined'。类型'FirebaseSession'缺少类型'MemoryStore'中的以下属性:get、set、destroy、touch
这是我遇到的错误编译时间好的,你必须现在就开始使用这个类型works@anny123如果要向库中添加类型,则必须使其与FirebaseSession返回的正确返回类型、参数等一起工作。要修复上述错误,可以导入
import{Store,MemoryStore}从“快速会话”;
将此行更改为导出类FirebaseSession扩展MemoryStore,但现在这是一个“技巧”,如果您正在键入库,您必须知道库返回的确切类型。因此,您应该阅读
connect session firebase
的代码,并根据需要键入它。@anny123有什么新闻吗?
interface firebaseSession {
    new(/*...param declarations here if appropriate...*/): firebaseSession;
    // ...
}

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;
}
import FirebaseSession from 'connect-session-firebase'

var firebase = new FirebaseSession({
 //params
});
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;
}