Postfix ! కంపైలర్కు "ఈ విలువ ఇక్కడ null కాదు లేదా undefined కాదని నేను గ్యారంటీ ఇస్తున్నాను" — runtime check ఏదీ లేకుండా null/undefined ను దాని రకం నుండి తీసివేస్తుంది.
() {
.(name!.());
}
Postfix ! కంపైలర్కు "ఈ విలువ ఇక్కడ null కాదు లేదా undefined కాదని నేను గ్యారంటీ ఇస్తున్నాను" — runtime check ఏదీ లేకుండా null/undefined ను దాని రకం నుండి తీసివేస్తుంది.
() {
.(name!.());
}
ఇది పూర్తిగా compile-time assertion — as వలె, ఇది ఎటువంటి runtime verification చేయదు. మీరు తప్పు అయితే, ఇది క్రాష్ అవుతుంది:
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
ఇవి ప్రతిটి నుండి దానిని నిర్ధారించే బదులు విల్లు కేసును నిర్వహిస్తుంది.
! ఒక తీక్ష్ణ సాధనం: ఇది కంపైలర్ మీకు ఇవ్వడానికి ప్రయత్నిస్తున్న null-safety ను నిశ్చేష్టం చేస్తుంది.
ఇది కొన్నిసార్లు సమర్థించబడుతుంది (కంపైలర్కు లేని జ్ఞానం మీకు ఉంది), కానీ దానిని ఓవర్యూజ్ చేయడం strictNullChecks నిరోధించే సరిగ్గా null-crash బగ్లను పునరంచనం చేస్తుంది.
ముందుగా ?., ??, లేదా స్పష్టమైన గార్డ్కు చేరుకోండి; non-nullness నిజంగా నిరూపించగలిగినప్పుడు మాత్రమే ! ఉపయోగించండి.