Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

524
Views
JWT: Argumento de tipo 'cadena | indefinido' no se puede asignar a un parámetro de tipo 'Secreto'

Estoy usando dotenv para declarar la variable env JWT_SECRET y muestra el error mencionado en el título.

.env

 NODE_ENV="development" JWT_SECRET="mySecretString"

medio ambiente.d.ts

 import { Secret } from 'jwt-promisify' declare global { namespace NodeJS { interface ProcessEnv { JWT_SECRET: Secret, NODE_ENV: 'development' | 'production', PORT?: number | string } } } export {}

Estoy usando en mi archivo de rutas estoy firmando token con JWT_SECRET

ruta.ts

 const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET)

Aquí intellisense funciona, pero cuando ejecuto la aplicación o la compilo, aparece el error.

error

 error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'. Type 'undefined' is not assignable to type 'Secret'. 32 const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET) ~~~~~~~~~~~~~~~~~~~~~~
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

 jwt.sign(data, process.env.SIGNATURE_KEY as string, { expiresIn: '30d', algorithm: "HS256" }, (err, encoded)=>{ err ? reject(err) : resolve(encoded) })

Para Typescript, creo que Type casting funciona. Tampoco implementé async-await porque el método de firma no se insinuó como Promise . ¡Pero supongo que también funciona!

over 4 years ago · Santiago Trujillo Report

0

El problema aquí es que typeof process.env.JWT_SECRET es una cadena o no está definido. Así que podrías hacer:

 const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET || "")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!