Il-postfix ! jgħid lill-compiler "Nigurantixxu li dan il-valur mhuwiex null jew undefined hawnhekk" — neħħi null/undefined mit-tip tiegħu mingħajr ebda verifika runtime.
ts
() {
.(name!.());
}
Il-postfix ! jgħid lill-compiler "Nigurantixxu li dan il-valur mhuwiex null jew undefined hawnhekk" — neħħi null/undefined mit-tip tiegħu mingħajr ebda verifika runtime.
() {
.(name!.());
}
Hija purament asserżjoni compile-time — bhal as, ma tfax l-ebda verifika runtime. Jekk inti ħtieġa, taqgħa:
const el = document.getElementById("app")!; // assert non-null
el.innerHTML = "hi"; // 💥 runtime error if #app doesn't actually exist
// 1. You've logically guaranteed it, but the compiler can't see it
if (map.has(key)) map.get(key)!.doThing(); // has() proves get() isn't undefined
// 2. Class fields initialized outside the constructor (DI, lifecycle hooks)
class C { value!: string; } // definite assignment assertion
name?.toUpperCase(); // optional chaining — no crash, yields undefined
const x = name ?? "default"; // provide a fallback
if (name) name.toUpperCase(); // narrow with a real check
Kull waħda minn dawn tindirizza il-każ li naqas minflok ma tassertax lilu.
! hija għodda mqaddsa: isemmixxi l-null-safety li l-compiler qed jipprova jigħtik.
Dimmo jkun justifikat (għandek għarfien li l-compiler m'ghandux), imma l-iżżejt tal-użu jerġa'jintroduċi eżattament il-bugs ta' null-crash li strictNullChecks jipprevjeni.
Isal għal ?., ??, jew guardia espliċita l-ewwel; uża ! biss meta verament tista' tippruva non-nullness.