Type guards (x is T)
Në shqip: Mbrojtësit e tipit
ShpjegimiSQ
Një type guard është funksioni yt që kthen true/false dhe i thotë TypeScript-it çfarë tipi është vlera — me sintaksën v is Lojtar. Pas kontrollit, TypeScript-i e trajton vlerën si atë tip.
EnglishEN
A type guard is your own function returning true/false that tells TypeScript what type a value is — with the syntax v is Player. After the check, TypeScript treats the value as that type.
Si ta mendosh
Si një test identiteti që, pasi e kalon, të jep kartën e duhur të hyrjes.
Lexoje në anglisht
Like an ID check that, once passed, gives you the right access badge.
Shembull koditypescript
function eshteLojtar(x: unknown): x is Lojtar {
return typeof x === "object" && x !== null && "emri" in x;
}