Passport Authenticator 抱怨 'this' 可以用任意类型实例化

Passport Authenticator complains of that 'this' could be instantiated with an arbitrary type

我正在尝试输入一个函数来接受本地和通用护照验证器。不幸的是,我遇到了“这个”Typescript 错误:

import { Passport, Authenticator } from 'passport';

const alternativePassport: Authenticator = new Passport();

导致错误:

Type 'import("/home/max/workspace/npm/graphql-passport/node_modules/@types/passport/index").Authenticator<import("/home/max/workspace/npm/graphql-passport/node_modules/@types/express/index").Handler, any, any, import("/home/max/workspace/npm/graphql-passport/node_modules/@types/passport/index").AuthenticateOptions>' is not assignable to type 'import("/home/max/workspace/npm/graphql-passport/node_modules/@types/passport/index").PassportStatic.Authenticator<import("/home/max/workspace/npm/graphql-passport/node_modules/@types/express/index").Handler, any, any, import("/home/max/workspace/npm/graphql-passport/node_modules/@types/passport/index").Authenticate...'.
  The types returned by 'use(...)' are incompatible between these types.
    Type 'Authenticator<Handler, any, any, AuthenticateOptions>' is not assignable to type 'this'.
      'this' could be instantiated with an arbitrary type which could be unrelated to 'Authenticator<Handler, any, any, AuthenticateOptions>'.ts(2322)
const alternativePassport: Authenticator<express.Handler, any, any, passport.AuthenticateOptions>
No quick fixes available

小注: 在实际代码中,我想将 alternativePassport 传递给一个带有可选护照对象的函数,如果对象不是,则该对象被分配给 passport.default 导出'提供。

Passport 的输入似乎有问题,或者 Authenticator 存在一些潜在问题。老实说,我不确定 TS 在抱怨什么,当我完全按照以前的方式重新定义类型时,只是重新声明 useunuse 错误消失了。我唯一的猜测是 passport 在某处覆盖了 Authenticator 界面或者存在打字稿错误:

interface WorkingAuthenticator<
  InitializeRet = express.Handler,
  AuthenticateRet = any,
  AuthorizeRet = AuthenticateRet,
  AuthorizeOptions = AuthenticateOptions
> extends Omit<Authenticator<InitializeRet, AuthenticateRet, AuthorizeRet, AuthorizeOptions>,  'use' | 'unuse'> {
  // This is exactly the same as the `Authenticator` interface
  use(strategy: Strategy): this;
  use(name: string, strategy: Strategy): this;
}

const auth: WorkingAuthenticator = new Passport();

passport Github 页面上的问题可能是解决此问题的方法,您可以使用 WorkingAuthernticator 类型直到问题得到解决或者有人正在处理 passport知道为什么会出现这个错误。