MINSEO KIM
MINSEO KIM

Reputation: 31

How to fix "No overload matches this call." error when using passport.serializeUser?

I'm developing passport.serializeUser with TypeScript.

passport.serializeUser((user: User, done) => {
    done(null, user.id)
});

This is the error I get:

No overload matches this call.
  Overload 1 of 2, '(fn: (user: User, done: (err: any, id?: unknown) => void) => void): void', gave the following error.
    Argument of type '(user: import("/Users/minseokim/Documents/dev/project/my-todo/models/user").default, done: (err: any, id?: unknown) => void) => void' is not assignable to parameter of type '(user: Express.User, done: (err: any, id?: unknown) => void) => void'.
      Types of parameters 'user' and 'user' are incompatible.
        Type 'User' is missing the following properties from type 'User': id, email, password, nickname, and 31 more.
  Overload 2 of 2, '(fn: (req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void): void', gave the following error.
    Argument of type '(user: User, done: (err: any, id?: unknown) => void) => void' is not assignable to parameter of type '(req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void'.
      Types of parameters 'user' and 'req' are incompatible.
        Type 'IncomingMessage' is missing the following properties from type 'User': id, email, password, nickname, and 30 more.

Upvotes: 3

Views: 609

Answers (1)

nakorndev
nakorndev

Reputation: 919

What to do is extend the interface on a declaration file to add more overload, by using this code into ./types/passport.d.ts file.

import { User } from '../models/Users' // export interface User { _id: string, ... }

declare module 'passport' {
  interface Authenticator {
    serializeUser<TID>(fn: (user: User, done: (err: any, id?: TID) => void) => void): void;
  }
}

and then config your ./tsconfig.json with this.

{
  "compilerOptions": {
    "typeRoots": ["./types"],
    ...
  }
}

Finally, your code should be work without error.

passport.serializeUser((user: User, done) => {
    done(null, user.id)
});

Keep in mind you also can use user: any instead to prevent this error.

Upvotes: 2

Related Questions