Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

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

$
0
0

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 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>