Skip to content

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

Description

@alexbjorlig

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 🤷🏽‍♂️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions