Estou usando o VS Code. É baseado em TypeScript, usa todas as definições de TS. O problema é com a biblioteca jsonwebtoken
. jwt.sign(...,)
não tem Object.
// Definições de tipo para jsonwebtoken 7.2.0
// Projeto: https://github.com/auth0/node-jsonwebtoken
// Definições por: Maxime LUCE https://github.com/SomaticIT , Daniel Heim https://github.com/danielheim
// Definições: https://github.com/DefinitelyTyped/DefinitelyTyped
// Versão TypeScript: 2.2
export declare function sign(payload: string | Buffer | object, secretOrPrivateKey: string | Buffer, options: SignOptions, callback: SignCallback): void;
No JWT você pode passar qualquer objeto que quiser, mas o simples jwt.sign({userId: 1},...)
não funciona, diz:
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'.
Deve ser corrigido por Microsoft/TypeScript#16290. Por favor, experimente com typescript@next
quando estiver dentro.
Como @andy-ms mencionou, isso se deve a um bug no TypeScript (Microsoft/TypeScript#16235)
A solução é usar inferência de tipo (ou conversão), por exemplo;
const payload = { "foo": "bar" }
jwt.sign(payload, secret)
@mdebruijne Obrigado, já fiz isso.
Comentários muito úteis
Como @andy-ms mencionou, isso se deve a um bug no TypeScript (Microsoft/TypeScript#16235)
A solução é usar inferência de tipo (ou conversão), por exemplo;