Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript "redefinition" errors in 4.1.0 #391

Closed
guyellis opened this issue Feb 23, 2021 · 8 comments
Closed

TypeScript "redefinition" errors in 4.1.0 #391

guyellis opened this issue Feb 23, 2021 · 8 comments

Comments

@guyellis
Copy link
Contributor

  • I'm submitting a ...
    [x] bug report

  • Summary

Just trying out connect-mongo@4.1.0. Thank you for converting this to TypeScript!

I'm getting a number of TypeScript transpiler errors from the definitions.

For example, in lib/MongoStore.d.ts the MongoStore extends session.Store and redefines get as:

get(sid: string, callback: (err: ErrorOrNull, session?: session.SessionData | null) => void): void;

MongoStore itself is extending (from express-session) session.Store. There get is defined as:

get: (sid: string, callback: (err: any, session?: Express.SessionData | null) => void) => void;

From that we get this TypeScript error:

"Class 'Store' defines instance member property 'get', but extended class 'MongoStore' defines it as instance member function.ts(2425)"

@mingchuno
Copy link
Collaborator

mingchuno commented Feb 23, 2021

@guyellis It is actually a bit tricky here. The TS definition of the store state that it is err: any but it is too general. In reality, it will only be Error | null. But since major store impl are not written in TS, so it does not matter before. May be I can change it back to any 🤔 Any way to bypass the error?

@guyellis
Copy link
Contributor Author

When you extend that class is it possible to use some sort of Omit<> trick that removes that definition completely and then allows you to redefine it?

Omit Utility Type: https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys

@guyellis
Copy link
Contributor Author

I just skimmed through this article that might have some clues on how to do this: https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c

@mingchuno mingchuno self-assigned this Feb 23, 2021
@mingchuno mingchuno added the bug label Feb 23, 2021
@mingchuno mingchuno added this to the 4.2 milestone Feb 23, 2021
@mingchuno
Copy link
Collaborator

@guyellis Try to align with the original typedef but it just doesn't work.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-session/index.d.ts#L342
https://github.com/expressjs/session#storetouchsid-session-callback
You can see the typedef of touch is:

callback?: () => void

But the doc said the implementation should call callback(error)

@mingchuno
Copy link
Collaborator

released in 4.2.0 please check

@Koslun
Copy link

Koslun commented Apr 29, 2021

@mingchuno
Still getting this error in version 4.4.1. So don't think this fixed it.

Some other dependencies in-use:

    "express": "4.17.1",
    "express-session": "1.17.1",
    "mongoose": "5.12.6",
    
     "@types/express": "4.17.1",
    "@types/express-session": "1.17.0",
    "typescript": "4.1.5"

Excerpt of usage in code:

import MongoStore from 'connect-mongo';
import mongoose = require('mongoose');

  app.use(
    session({
      secret: config.secrets.session,
      saveUninitialized: true,
      resave: false,
      store: MongoStore.create({
        client: mongoose.connection.getClient(),
      }),
    }),
  );

Error log:

> tsc

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:56:5 - error TS2425: Class 'Store' defines instance member property 'get', but extended class 'MongoStore' defines it as instance member function.

56     get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void;
       ~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:56:61 - error TS2694: Namespace 'session' has no exported member 'SessionData'.

56     get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void;
                                                               ~~~~~~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:62:5 - error TS2425: Class 'Store' defines instance member property 'set', but extended class 'MongoStore' defines it as instance member function.

62     set(sid: string, session: session.SessionData, callback?: (err: any) => void): void;
       ~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:62:39 - error TS2694: Namespace 'session' has no exported member 'SessionData'.

62     set(sid: string, session: session.SessionData, callback?: (err: any) => void): void;
                                         ~~~~~~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:63:5 - error TS2425: Class 'Store' defines instance member property 'touch', but extended class 'MongoStore' defines it as instance member function.

63     touch(sid: string, session: session.SessionData & {
       ~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:63:41 - error TS2694: Namespace 'session' has no exported member 'SessionData'.

63     touch(sid: string, session: session.SessionData & {
                                           ~~~~~~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:69:5 - error TS2416: Property 'all' in type 'MongoStore' is not assignable to the same property in base type 'Store'.
  Type '(callback: (err: any, obj?: any[] | { [sid: string]: any; }) => void) => void' is not assignable to type '(callback: (err: any, obj?: { [sid: string]: SessionData; }) => void) => void'.
    Types of parameters 'callback' and 'callback' are incompatible.
      Types of parameters 'obj' and 'obj' are incompatible.
        Type 'any[] | { [sid: string]: any; }' is not assignable to type '{ [sid: string]: SessionData; }'.
          Type 'any[]' is not assignable to type '{ [sid: string]: SessionData; }'.
            Index signature is missing in type 'any[]'.

69     all(callback: (err: any, obj?: session.SessionData[] | {
       ~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:69:5 - error TS2425: Class 'Store' defines instance member property 'all', but extended class 'MongoStore' defines it as instance member function.

69     all(callback: (err: any, obj?: session.SessionData[] | {
       ~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:69:44 - error TS2694: Namespace 'session' has no exported member 'SessionData'.

69     all(callback: (err: any, obj?: session.SessionData[] | {
                                              ~~~~~~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:70:32 - error TS2694: Namespace 'session' has no exported member 'SessionData'.

70         [sid: string]: session.SessionData;
                                  ~~~~~~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:76:5 - error TS2425: Class 'Store' defines instance member property 'destroy', but extended class 'MongoStore' defines it as instance member function.

76     destroy(sid: string, callback?: (err: any) => void): void;
       ~~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:80:5 - error TS2425: Class 'Store' defines instance member property 'length', but extended class 'MongoStore' defines it as instance member function.

80     length(callback: (err: any, length: number) => void): void;
       ~~~~~~

node_modules/connect-mongo/build/main/lib/MongoStore.d.ts:84:5 - error TS2425: Class 'Store' defines instance member property 'clear', but extended class 'MongoStore' defines it as instance member function.

84     clear(callback?: (err: any) => void): void;
       ~~~~~

Found 13 errors.

@mingchuno
Copy link
Collaborator

@Koslun Try to upgrade the @types/express-session to 1.17.3?

@Koslun
Copy link

Koslun commented Jul 2, 2021

@mingchuno

It did work now, might've been @types/express-session to 1.17.3 like you said but related changes I also made:

"dependencies": {
    // ...other deps
    "express": "4.17.1",
    "express-form-data": "2.0.17",
    "express-jwt": "6.0.0",
    "express-session": "1.17.2",
    "mongoose": "5.12.7",
    // ...other deps
  },
  "devDependencies": {
    // ...other deps
    "@types/express": "4.17.12",
    "@types/express-session": "1.17.3",
    "@types/node": "^12.0.7",
    "typescript": "4.3.4"
    // ...other deps
  },
  "resolutions": {
    "@types/serve-static": "1.13.9"
  },

The last resolution is for another issue: DefinitelyTyped/DefinitelyTyped#49595 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants