egg-jwt
egg-jwt copied to clipboard
typescript中 , app.jwt 作为中间件使用,类型不匹配报错了
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)
作为中间件使用时, 类型不匹配报错了
实际返回Object,但接口定义是返回string
怎么修改下呀
怎么搞,有人处理吗
解决了吗?应该怎么搞
遇到相同的问题
提供个比较挫的解决办法,在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
提供个比较挫的解决办法,在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
}
}
提供个比较挫的解决办法,在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)