Recientemente aprendí mecanografiado genérico. y lo usé en ciertas situaciones. pero no pude
Cuando usaba dos interfaces diferentes, lo quería.
por ejemplo
Quiero enviar una solicitud de servidor.
function fetcher async(){ return await axios.get(url).then(data=>data.result) }
y el servidor me da respuestas pero tienen dos tipos diferentes
// response A interfase AInterface { name: string; age: number; address: string } // response B interfase BInterface { name: string; job : string; haveCar: boolean; }
y establezca el tipo de retorno usando genérico
function fetcher<T> async():<T>{ return await axios.get(url).then(data=>data.result) } and use function const result = fetcher<AInterface>() or fetcher<BInterface>()
resultado tiene tipo AInterface | BInterfaz
por qué el resultado tiene tipo AInterface | BInterfaz ??
no sé
quiero que tenga AInterface o BInterface. no unión
No es necesario escribir 'buscador'
Solo usa:
async function fetcher() { const { data } = await axios.get<AInterface>(url) return data }
O:
async function fetcher(): Promise<AInterface> { const { data } = await axios.get(url) return data }