{ done(-6ren">
gpt4 book ai didi

typescript - 使用 passport.serializeUser 时如何修复 "No overload matches this call."错误?

转载 作者:行者123 更新时间:2023-12-05 06:02:46 26 4
gpt4 key购买 nike

我正在使用 TypeScript 开发 passport.serializeUser

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

这是我得到的错误:

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.

最佳答案

要做的是在声明文件上扩展接口(interface)以添加更多重载,方法是将此代码用于 ./types/passport.d.ts 文件。

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

然后用这个配置你的./tsconfig.json

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

最后,您的代码应该可以正常运行。

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

请记住,您也可以使用 user: any 来防止出现此错误。

关于typescript - 使用 passport.serializeUser 时如何修复 "No overload matches this call."错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66849999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com