Definitelytyped: [@types/cookie-session] Types of session are incompatible

Created on 15 Jul 2018  路  5Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x ] I tried using the @types/cookie-session package and had problems.

Using with express only works well.
When sharing the session with socket.io, the typescript compiler fails with:

node_modules/@types/cookie-session/index.d.ts:10:15 - error TS2430: Interface 'Request' incorrectly extends interface 'CookieSessionRequest'.
  Types of property 'session' are incompatible.
    Type 'Session' is not assignable to type 'CookieSessionObject'.
      Property 'isChanged' is missing in type 'Session'.

10     interface Request extends CookieSessionInterfaces.CookieSessionRequest {}
                 ~~~~~~~

Implementation:

      const session = cookieSession({
         name: 'myName',
         keys: ['123456789'],
         secret: '112233',

         // Cookie Options
         maxAge: 24 * 60 * 60 * 1000 * 30, // 30 days
      });

      app.use(session);
      socket.use(sharedSession(session));
  • "typescript": "2.9.2"
  • Authors: @borislavjivkov

Most helpful comment

I was able to workaround this issue by forcing @types/cookie-session to version 2.0.37.

All 5 comments

I'm not sure if this is the correct solution, but if so you may merge my pull request.
I was able to fix the issue by editing cookie-session/index.d.ts:

  • isChanged, isNew, isPopulated, session now marked as optional, to correctly match the source type Request.
 interface CookieSessionObject {
        /**
         * Is true if the session has been changed during the request.
         */
        isChanged?: boolean;

        /**
         * Is true if the session is new.
         */
        isNew?: boolean;

        /**
         * Determine if the session has been populated with data or is empty.
         */
        isPopulated?: boolean;

        [propertyName: string]: any;
    }

    interface CookieSessionRequest {
        /**
         * Represents the session for the given request.
         */
        session?: CookieSessionObject;

        /**
         * Represents the session options for the current request. These options are a shallow clone of what was provided at middleware construction and can be altered to change cookie setting behavior on a per-request basis.
         */
        sessionOptions: CookieSessionOptions;
    }

Encountering very similar error and this was the only relevant result google found.

node_modules/@types/cookie-session/index.d.ts:11:15 - error TS2430: Interface 'Request' incorrectly extends interface 'CookieSessionRequest'.
  Types of property 'session' are incompatible.
    Type 'Session | undefined' is not assignable to type 'CookieSessionObject | null'.
      Type 'undefined' is not assignable to type 'CookieSessionObject | null'.

11     interface Request extends CookieSessionInterfaces.CookieSessionRequest {}
                 ~~~~~~~


Found 1 error.

I was able to workaround this issue by forcing @types/cookie-session to version 2.0.37.

Thank you @kaaloo, lifesaver!

Omg I wasted about 3 hours on this @kaaloo thanks!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tyv picture tyv  路  3Comments

JudeAlquiza picture JudeAlquiza  路  3Comments

jamespero picture jamespero  路  3Comments

Loghorn picture Loghorn  路  3Comments

demisx picture demisx  路  3Comments