Definitelytyped: JWT

Created on 6 Jun 2017  ·  3Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

I'm using VS Code. It's based on TypeScript, it uses all TS definitions. The problem is with jsonwebtoken library. jwt.sign(...,) doesn't have Object.

// Type definitions for jsonwebtoken 7.2.0
// Project: https://github.com/auth0/node-jsonwebtoken
// Definitions by: Maxime LUCE https://github.com/SomaticIT, Daniel Heim https://github.com/danielheim
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2

export declare function sign(payload: string | Buffer | object, secretOrPrivateKey: string | Buffer, options: SignOptions, callback: SignCallback): void;

In JWT you can pass any object you want, but simple jwt.sign({userId: 1},...) doesn't work it says:

Argument of type '{ userId: number; }' is not assignable to parameter of type 'string | object | Buffer'.
  Object literal may only specify known properties, and 'userId' does not exist in type 'string | object | Buffer'.

Most helpful comment

As @andy-ms mentioned, this is due to a bug in TypeScript (Microsoft/TypeScript#16235)

The workaround is to use type inference (or casting), for example;

const payload = { "foo": "bar" }

jwt.sign(payload, secret)

All 3 comments

Should be fixed by Microsoft/TypeScript#16290. Please try it out with typescript@next once that's in.

As @andy-ms mentioned, this is due to a bug in TypeScript (Microsoft/TypeScript#16235)

The workaround is to use type inference (or casting), for example;

const payload = { "foo": "bar" }

jwt.sign(payload, secret)

@mdebruijne Thanks, already did that.

Was this page helpful?
0 / 5 - 0 ratings