Definitelytyped: Errors with promisify in jsonwebtoken

Created on 9 Sep 2019  路  3Comments  路  Source: DefinitelyTyped/DefinitelyTyped

Issue Information

  • [x] I tried using the @types/jsonwebtoken package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [ ] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors:

    • @SomaticIT

    • @danielheim

    • @brikou

    • @vpk

    • @rlgod

    • @kettil

Description of the problem

After making the promise he gives me these mistakes. The truth is that I'm not sure if they are the types


TS2322: Type 'unknown' is not assignable to type 'UserProfile'

import {
  sign as signToken,
  verify as verifyToken
} from 'jsonwebtoken'

const verify = promisify(verifyToken);

let userProfile: UserProfile;
userProfile = await verify(token, this.jwtSecret);

TS2554: Expected 2 arguments, but got 3

import {
  sign as signToken,
  verify as verifyToken
} from 'jsonwebtoken'

const sign = promisify(signToken);

let token: string;
token = await sign(userInfoForToken, this.jwtSecret, {
  expiresIn: Number(this.jwtExpiresIn),
});

Most helpful comment

There's a few issues here.

  1. The verify function callback is non standard in that it will be invoked with either the error or the result in the same argument position. promisify requires the function signature function(err, result).
  2. You would need to use the generic version of promisify which I think in the case of sign with the three parameters is promisify<string, Secret, VerifyOptions, UserProfile>(sign) or something like that.

I fixed the problem by changing import torequire

All 3 comments

There's a few issues here.

  1. The verify function callback is non standard in that it will be invoked with either the error or the result in the same argument position. promisify requires the function signature function(err, result).
  1. You would need to use the generic version of promisify which I think in the case of sign with the three parameters is promisify<string, Secret, VerifyOptions, UserProfile>(sign) or something like that.

There's a few issues here.

  1. The verify function callback is non standard in that it will be invoked with either the error or the result in the same argument position. promisify requires the function signature function(err, result).
  2. You would need to use the generic version of promisify which I think in the case of sign with the three parameters is promisify<string, Secret, VerifyOptions, UserProfile>(sign) or something like that.

I fixed the problem by changing import torequire

I fixed the problem by changing import to require

Doesn't this mean that it would import promisify as an any type, thus ignoring the definitions?

Was this page helpful?
0 / 5 - 0 ratings