TypeScript

Typing API responses

Në shqip: Tipet për përgjigjet e API-së

ShpjegimiSQ

Kur merr të dhëna me fetch, TypeScript-i s'e di formën e tyre. I jep tipin vetë (Promise<Termi[]>), dhe për siguri të plotë i kontrollon me Zod — ose i gjeneron tipet automatikisht nga dokumentimi OpenAPI.

EnglishEN

When you fetch data, TypeScript doesn't know its shape. You give it the type yourself (Promise<Term[]>), and for full safety you check it with Zod — or generate the types automatically from the OpenAPI docs.

Si ta mendosh

Si t'i thuash korrierit çfarë duhet të ketë pakoja — dhe ta kontrollosh kur arrin.

Lexoje në anglisht

Like telling the courier what the parcel should contain — and checking when it arrives.

Shembull koditypescript

async function merr<T>(url: string): Promise<T> {
  const r = await fetch(url);
  if (!r.ok) throw new Error(String(r.status));
  return r.json() as Promise<T>;
}

Terma të lidhur