Monday, 25 November 2019

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 3: 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

declare module "FirebaseSession" {
  import * as admin from 'firebase-admin'
  import { Request, Response, NextFunction } from "express";
  export interface OptionsShape {
    database: admin.database.Database | admin.firestore.Firestore, 
    sessions?: string, 
    reapInterva?: string,
    errorIfSessionNotFound?: any
  }
  export class FirebaseSession {
    constructor (options: OptionsShape)
  }
}

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

 const connect_session_firebase_1 = require("connect-session-firebase");

     app.use(express_session_1.default({
                store: new connect_session_firebase_1.FirebaseSession({
                    database: context.realtimeDB
                }),
                name: '__session',
                secret: 'kkkkj9h9',
                resave: true,
                saveUninitialized: true
            }));

which throws an error connect_session_firebase_1.FirebaseSession is not a constructor



from Creating Constructor signature

No comments:

Post a Comment