Node-jsonwebtoken: TS2326: Types of property 'sub' are incompatible. Type 'string' is not assignable to type '() => string'.

Created on 4 Jun 2019  ·  7Comments  ·  Source: auth0/node-jsonwebtoken

I don't know if this is a typings bug, or a library issue.

With the following versions:
"@types/jsonwebtoken": "^8.3.0"
"jsonwebtoken": "^8.3.0"

The following snippet:

const jsonWebToken = jwt.sign({
            user_id, // The userID
            sub: 'some-sub', // The id from above
            ulimit: 2 // The usage limit
        }, 'shh');

throws the error:

TS2326: Types of property 'sub' are incompatible.
  Type 'string' is not assignable to type '() => string'.

But if I look at the typings:

export function sign(
    payload: string | Buffer | object,
    secretOrPrivateKey: Secret,
    options?: SignOptions,
): string;

It looks correct?

Workaround:

const jsonWebToken = jwt.sign({
            user_id, // The userID
            sub: 'some-sub', // The id from above
            ulimit: 2 // The usage limit
        } as any, 'shh');

If the sub property indeed needs to be () => string I would happily like to assist with the PR to the typings, but I am not sure what the problem is 🤷🏽‍♂️

Most helpful comment

Looks like the bug is fixed in the pending next version of TypeScript. Installing typescript@next (version 3.6.0-dev.20190623) makes the compilation work. We just need to wait for the next release and build with 3.4 for now.

All 7 comments

I also got the problem.
You can cast it sub: 'some-sub' as any. Temporary solution

const jsonWebToken = jwt.sign({
            user_id, // The userID
            sub: 'some-sub' as any, // The id from above
            ulimit: 2 // The usage limit
        } as any, 'shh');

It looks like it is a compiler issue. It works with TypeScript 3.4.5 but fails with TypeScript 3.5.1 & 3.5.2.

@LoicPoullain good catch - but why does it fail with Typescript > 3.5.1 🤷🏽‍♂️ ?

No idea 😕 I'm going to open an issue on the TS repo today when I get a chance.

Looks like the bug is fixed in the pending next version of TypeScript. Installing typescript@next (version 3.6.0-dev.20190623) makes the compilation work. We just need to wait for the next release and build with 3.4 for now.

I'm going to go ahead and close this as a) it's resolved, b) there's no TODO in this repo or the types one, which we're not the maintainers of btw.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

usamamashkoor picture usamamashkoor  ·  4Comments

rockchalkwushock picture rockchalkwushock  ·  4Comments

ngminhduong picture ngminhduong  ·  3Comments

BarukhOr picture BarukhOr  ·  4Comments

samholmes picture samholmes  ·  5Comments