Assertion functions (asserts)
Në shqip: Funksionet pohuese
ShpjegimiSQ
Një funksion pohues hedh gabim nëse kushti s'plotësohet, dhe pas thirrjes TypeScript-i di që kushti është i vërtetë: function sigurohu(x: unknown): asserts x is string.
EnglishEN
An assertion function throws if a condition fails, and after the call TypeScript knows the condition holds: function ensure(x: unknown): asserts x is string.
Si ta mendosh
Si roja që të ndal te dera nëse s'ke biletë — po kalove, je i sigurt brenda.
Lexoje në anglisht
Like a doorman who stops you if you have no ticket — once you're past, you're definitely in.
Shembull koditypescript
function sigurohu(x: unknown): asserts x is string {
if (typeof x !== "string") throw new Error("S'është tekst");
}
sigurohu(v); v.toUpperCase(); // tani v është string