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

96
Views
Rechazar promesa con cadena en función que tiene un tipo de retorno diferente

Tengo una función así:

 invoke(token: string): Promise<IUserDto> { return new Promise((reject, resolve) => { // Tokens are generally passed in the header of the request // Due to security reasons. let jwtSecretKey = process.env.JWT_SECRET_KEY; try { const verified = jwt.verify(token, jwtSecretKey!); if(verified){ resolve(verified); }else{ // Access Denied reject(); } } catch (error) { // Access Denied reject(); } }); }

Y estoy tratando de probarlo así:

 it('Validate user token use case', async () => { let jwtSecretKey = process.env.JWT_SECRET_KEY; let userData: IUserDto = { id: 'testid', firstName: 'firstName', lastName: 'lastName', username: 'username', email: 'test@test.com', password: 'testpassword', birthDate: new Date('0'), reports: [ new Report('report_id', 'report data', 0) ] }; let token = jwt.sign(userData, jwtSecretKey!); let validateUserTokenUseCase = new ValidateUserTokenUseCase(); let validated: IUserDto | String = await validateUserTokenUseCase.invoke(token); expect(validated.id).toBe(userData.id); })

No puedo hacer esto porque al rechazar la promesa en la función, no devuelve un objeto de IUserDto, devuelve cualquier cadena de error que puse en el rechazo.

He intentado definir el tipo de devolución como Promise<IUserDto | String> , pero mi IDE no lo detecta bien, no puedo ver, por ejemplo, validated.id cuando lo escribo en mi IDE, lo cual es molesto.

¿Hay alguna forma de rechazar este método sin definir el tipo de promesa como IUserDto | String ?

about 4 years ago · Juan Pablo Isaza
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!