类型错误:类构造函数 MixinStrategy 不能在没有“new”的情况下被调用

wen*_*mva 6 jwt typescript passport.js nestjs

我正在关注 jwt 示例,例如在https://docs.nestjs.com/techniques/authentication 中找到的示例。我复制并粘贴了这个例子。在 npm 安装必要的位和 bops 后,我得到了这个错误,这在我刚刚复制的示例中没有发生。其中我不知道这是什么意思!有人有什么想法吗?

TypeError: Class constructor MixinStrategy cannot be invoked without 'new'

   8 | export class JwtStrategy extends PassportStrategy(Strategy) {
   9 |   constructor(private readonly authService: AuthService) {
> 10 |     super({
  11 |       jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  12 |       secretOrKey: 'secretKey',
  13 |     });

  at new JwtStrategy (data/auth/strategies/jwt.strategy.ts:10:5)
  at resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:64:84)
  at Injector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:86:30)
Run Code Online (Sandbox Code Playgroud)

Est*_*ask 5

项目缺少@types/passport-jwt类型,因此应另外安装:

npm i -D @types/passport-jwt
Run Code Online (Sandbox Code Playgroud)

这导致

src\auth\jwt.strategy.ts (10,6):调用目标不包含任何签名。(2346)

错误,因为@nestjs/passport输入不正确;PassportStrategy返回类型是any.

为了解决这个问题,

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
...
Run Code Online (Sandbox Code Playgroud)

应改为:

import { ExtractJwt, Strategy } from 'passport-jwt';
import { AbstractStrategy, PassportStrategy } from '@nestjs/passport';
...
const PassportJwtStrategy: new(...args) => AbstractStrategy & Strategy = PassportStrategy(Strategy);

@Injectable()
export class JwtStrategy extends PassportJwtStrategy {
...
Run Code Online (Sandbox Code Playgroud)