Result types instead of throwing
Në shqip: Tipi Result në vend të përjashtimeve
ShpjegimiSQ
Në vend që të hedhësh gabime, disa projekte kthejnë { ok: true, vlera } ose { ok: false, gabimi }. TypeScript të detyron t'i trajtosh të dyja rastet — si Result-i i Rust-it.
EnglishEN
Instead of throwing, some projects return { ok: true, value } or { ok: false, error }. TypeScript forces you to handle both cases — like Rust's Result.
Si ta mendosh
Si një zarf që ka gjithmonë ose çekun ose shpjegimin pse s'ka çek.
Lexoje në anglisht
Like an envelope that always holds either the cheque or the reason there's no cheque.
Shembull koditypescript
type Rezultat<T> = { ok: true; vlera: T } | { ok: false; gabimi: string };
function ndaj(a: number, b: number): Rezultat<number> {
return b === 0 ? { ok: false, gabimi: "Pjesëtim me 0" } : { ok: true, vlera: a / b };
}