Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Constructor signature

I am new to typescript, migrating my existing js code to ts code.

Apparently connect-session-firebase does not have custom typing so I decided to create one..

this is how I am using it in my code

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
            })
        )

Where this is my typing

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
  }
}

And I know this is wrong because here store: new firebaseSession({ I am getting following error

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

Can someone tell me what I am doing wrong and How I can fix it?

Update: I updated firebaseSession to FirebaseSession and tried with new() but still no luck

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)
  }
}

but same error

Update : Here is my updated code:

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
            })
        )

with types 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

Note while this doesn't throw an error on compile time, it creates equivalent JS code like this

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
        }));

which throws an error connect_session_firebase_1.FirebaseSession is not a constructor

Cannot read property prototype of undefined.

like image 937
iRohitBhatia Avatar asked Jan 21 '26 03:01

iRohitBhatia


1 Answers

Can you try this like this ?

1) create a type.d.ts with


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 your module and use it (it should be typed and not complain about type any

import FirebaseSession from 'connect-session-firebase'

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

EDIT : As I say in my comment, you can fix the compile like this, but the thing is now it's up to you to make the correct types for the libraries. I do not know the library, so I don't know what is the return value of new FirebaseSession({}), or what FirebaseSession has as properties and method...

You have to read the package and adapt your types.d.ts to it, so that it works flawlessly. You can probably get help from the package creator, and make a @types/connect-session-firebase when done. But if it's just for the compiler to work, you can keep patching (but is not recommended)

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;
}
like image 123
Crocsx Avatar answered Jan 22 '26 17:01

Crocsx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!