Aprendo TS, y he visto este código. Hay un código como este:
type FetchPokemonResult<T> = T extends undefined ? Promise<PokemonResults> : void;Devuelve una matriz de datos json. Entonces, ¿por qué eso no es Promise<PokemonResults[]> ? ¿Por qué es sin una matriz?
código
interface PokeArr { name: string; url: string; } interface PokemonResults { count: number; next: string; previous: null; results: PokeArr[]; }; type FetchPokemonResult<T> = T extends undefined ? Promise<PokemonResults> : void; function fetchPokemon<T extends undefined | ((data: PokemonResults) => void)>(url: string, cb?: T): FetchPokemonResult<T> { if(cb) { fetch(url) .then(res => res.json()) .then(cb) return undefined as FetchPokemonResult<T>; } else { return fetch(url).then(res => res.json()) as FetchPokemonResult<T> } }Se escribe de esta manera porque la función admite proporcionar una devolución de llamada cb o devolver una promesa.
// typeof e is PokemonResults fetchPokemon("", (e => console.log(e))); // typeof e is PokemonResults fetchPokemon("")?.then(e => console.log(e));Devuelve una matriz de datos json. Entonces, ¿por qué eso no es Promise<PokemonResults[]> ? ¿Por qué es sin una matriz?
No parece devolver una matriz. Según su pregunta, devuelve un PokemonResults que tiene una lista de results .