Quantcast
Viewing all articles
Browse latest Browse all 6404

TS Why I dont set an array at Promise in fetch?

I learn TS, and I have seen this code. There is a code like this:

type FetchPokemonResult<T> = T extends undefined ? Promise<PokemonResults> : void;

It retuns a array of json data. So Why is that not Promise<PokemonResults[]> ? Why is it without an array ?

code

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>  }}

Viewing all articles
Browse latest Browse all 6404

Trending Articles