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

210
Views
js fetch con TS, no es asignable

¿Por qué recibo esta advertencia en TS?

 Type 'unknown' is not assignable to type 'PokemonList'.ts(2322)

Este error está en línea: "return e"

Código:

 export interface PokemonList { count: number; next: string; previous?: any; results: { name: string; url: string; }[] }; const PokeApi = async (): Promise<PokemonList> => { try { const res = await fetch('https://pokeapi.co/api/v2/pokemon?limit=10'); return res.json() } catch(e) { return e; } };

Si hago esto:

 ( Promise<PokemonList> | unknown )

entonces me sale este error:

 The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<unknown>'?ts(
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El tipo de retorno de esa función es

 Promise<PokemonList | Error>

Como dijo Keith, esto es "No es una buena idea, al hacer esto ahora has roto tu cadena de promesas". Las promesas tienen .catch y funcionan en try/catch para encadenar las promesas. Lo que tiene requiere un control if(/else) , que no es para lo que están destinadas las Promesas.

Para solucionar esto, debe eliminar el bloque try/catch dentro de la función y simplemente manejar los errores con catch cada vez que lo llame.

 const PokeApi = async (): Promise<PokemonList> => { const res = await fetch('https://pokeapi.co/api/v2/pokemon?limit=10'); return res.json() }; // when calling it inside async function let pokemonList: PokemonList; try { pokemonList = await PokeApi(); } catch (e) { // Handle error }
about 4 years ago · Juan Pablo Isaza 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!