Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

529
Visualizações
JWT: Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'

I'm using dotenv to declare JWT_SECRET env variable and It's showing the error mentioned in title.

.env

NODE_ENV="development"
JWT_SECRET="mySecretString"

environment.d.ts

import { Secret } from 'jwt-promisify'

declare global {
    namespace NodeJS {
        interface ProcessEnv {
            JWT_SECRET: Secret,
            NODE_ENV: 'development' | 'production',
            PORT?: number | string
        }
    }
}

export {}

I'm using in my routes file im signing token with JWT_SECRET

route.ts

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

Here intellisense is working but when I run the app or compile it the error appears.

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 Respostas
Responde à pergunta

0

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

For Typescript, I think Type casting works. I also didn't implement async-await because the sign method wasn't hinted as a Promise. But I guess it works too!

over 4 years ago · Santiago Trujillo Relatório

0

The problem here is that typeof process.env.JWT_SECRET is either string or undefined. So you could do:

const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET || "")
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda