async/await
async function fetchTitle(): Promise<string> {
return "ok";
}
fetch api
const res = await fetch("/api/projects");
const data = await res.json() as Project[];
tipi base
let year: number = 2026;
let name: string = "Samir";
union types
type Id = string | number;
esercizi
crea un client api con fetch tipizzato.
interface Project { id: number; name: string }
const res = await fetch("/api/projects");
const data = (await res.json()) as Project[];
console.log(data.length);
panoramica
In questo capitolo su async e fetch lavoriamo su async/await, fetch api, tipi base, union types per creare micro-pattern riutilizzabili.
lavora in sequenza: leggi, prova, modifica gli snippet e annota i trade-off principali.
badge: typescript essentials
obiettivi
- capire e applicare async/await in uno scenario reale
- capire e applicare fetch api in uno scenario reale
- capire e applicare tipi base in uno scenario reale
- capire e applicare union types in uno scenario reale
scheda rapida
async function fetchTitle(): Promise<string> {
return "ok";
}
adatta questo scheletro agli esempi della lezione.
tips
- attiva strict
- evita any
- usa type alias chiari
- separa layer e dto
- mantieni funzioni pure
- gestisci timeout e cancellazioni
tip: documenta ogni scelta architetturale.
mini progetto
crea un client fetch tipizzato.
- definisci interface
- fetch
- parse json
output atteso: client funzionante.
checklist
- ho eseguito gli snippet e controllato l'output
- ho annotato almeno 2 trade-off
- ho completato il mini progetto
- ho salvato un riepilogo personale