egg-jwt icon indicating copy to clipboard operation
egg-jwt copied to clipboard

typescript中 , app.jwt 作为中间件使用,类型不匹配报错了

Open whwnow opened this issue 4 years ago • 8 comments

const jwt: {
    sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string;
    verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string;
    decode(token: string): string;
}
No overload matches this call.
  Overload 1 of 4, '(name: string, path: string | RegExp, ...middleware: Middleware<ParameterizedContext<any, Context & IRouterParamContext<any, Context>>>[]): Router<...>', gave the following error.
    Argument of type '{ sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string; verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string; decode(token: string): string; }' is not assignable to parameter of type 'string | RegExp'.
      Type '{ sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string; verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string; decode(token: string): string; }' is missing the following properties from type 'RegExp': exec, test, source, global, and 12 more.
  Overload 2 of 4, '(path: string | RegExp | (string | RegExp)[], ...middleware: Middleware<ParameterizedContext<any, Context & IRouterParamContext<any, Context>>>[]): Router<...>', gave the following error.
    Argument of type '{ sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string; verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string; decode(token: string): string; }' is not assignable to parameter of type 'Middleware<ParameterizedContext<any, Context & IRouterParamContext<any, Context>>>'.
      Type '{ sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string; verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string; decode(token: string): string; }' provides no match for the signature '(context: ParameterizedContext<any, Context & IRouterParamContext<any, Context>>, next: Next): any'.
  Overload 3 of 4, '(path: string | RegExp | (string | RegExp)[], middleware: Middleware<ParameterizedContext<unknown, unknown>>, routeHandler: Middleware<ParameterizedContext<any, Context & IRouterParamContext<...>>>): Router<...>', gave the following error.
    Argument of type '{ sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string; verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string; decode(token: string): string; }' is not assignable to parameter of type 'Middleware<ParameterizedContext<unknown, unknown>>'.
      Type '{ sign(payload: string | object | Buffer, secretOrPrivateKey: string, options?: any, callback?: any): string; verify(token: string, secretOrPrivateKey: string, options?: any, callback?: any): string; decode(token: string): string; }' provides no match for the signature '(context: ParameterizedContext<unknown, unknown>, next: Next): any'.ts(2769)

作为中间件使用时, 类型不匹配报错了

whwnow avatar Mar 15 '20 13:03 whwnow

实际返回Object,但接口定义是返回string

dropbug avatar Jul 21 '20 07:07 dropbug

怎么修改下呀

collinsyu avatar Sep 04 '20 06:09 collinsyu

怎么搞,有人处理吗

jok311 avatar Nov 09 '20 10:11 jok311

解决了吗?应该怎么搞

sinceseason avatar Feb 01 '21 05:02 sinceseason

遇到相同的问题

GideonSenku avatar Feb 26 '21 02:02 GideonSenku

提供个比较挫的解决办法,在typings/index.d.ts文件中添加如下代码:

import 'egg';

declare module 'egg' {
    interface EggJwt<StateT, CustomT> extends Router.IMiddleware<StateT, CustomT> {
      sign(
          payload: string | Buffer | object,
          secretOrPrivateKey: string,
          options?: SignOptions,
          callback?: SignCallback
      ): string;

      verify(token: string, secretOrPrivateKey: string, options?: VerifyOptions, callback?: VerifyCallback): string;

      decode(token: string): string;
  }

  interface Application {
      jwt: EggJwt
  }
}

原理就是将作者的app.jwt包装成一个类型,然后这个类型继承自Router.IMiddleware

Eybin avatar Jul 04 '21 15:07 Eybin

提供个比较挫的解决办法,在typings/index.d.ts文件中添加如下代码:

import 'egg';

declare module 'egg' {
    interface EggJwt<StateT, CustomT> extends Router.IMiddleware<StateT, CustomT> {
      sign(
          payload: string | Buffer | object,
          secretOrPrivateKey: string,
          options?: SignOptions,
          callback?: SignCallback
      ): string;

      verify(token: string, secretOrPrivateKey: string, options?: VerifyOptions, callback?: VerifyCallback): string;

      decode(token: string): string;
  }

  interface Application {
      jwt: EggJwt
  }
}

原理就是将作者的app.jwt包装成一个类型,然后这个类型继承自Router.IMiddleware

再精简一下:

import 'egg';

declare module 'egg' {

    interface Application {
        jwt: Router.IMiddleware 
    }
}

Eybin avatar Jul 04 '21 16:07 Eybin

提供个比较挫的解决办法,在typings/index.d.ts文件中添加如下代码:

import 'egg';

declare module 'egg' {
    interface EggJwt<StateT, CustomT> extends Router.IMiddleware<StateT, CustomT> {
      sign(
          payload: string | Buffer | object,
          secretOrPrivateKey: string,
          options?: SignOptions,
          callback?: SignCallback
      ): string;

      verify(token: string, secretOrPrivateKey: string, options?: VerifyOptions, callback?: VerifyCallback): string;

      decode(token: string): string;
  }

  interface Application {
      jwt: EggJwt
  }
}

原理就是将作者的app.jwt包装成一个类型,然后这个类型继承自Router.IMiddleware

再精简一下:

import 'egg';

declare module 'egg' {

    interface Application {
        jwt: Router.IMiddleware 
    }
}

对于service/controller 中使用 就会失去 提示(如jwt.sign)

notbucai avatar Mar 20 '22 10:03 notbucai