postfix ! कम्पाइलरलाई भन्छ "म यो मूल्य यहाँ null वा undefined छैन भनी ग्यारेन्टी दिन्छु" — कुनै पनि runtime जाँच बिना null/undefined यसको प्रकारबाट हटाउँछ।
ts
() {
.(name!.());
}
postfix ! कम्पाइलरलाई भन्छ "म यो मूल्य यहाँ null वा undefined छैन भनी ग्यारेन्टी दिन्छु" — कुनै पनि runtime जाँच बिना 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 bugs पुनः परिचय गराउँछ।
पहिले ?., ??, वा स्पष्ट गार्डको लागि पहुँच गर्नुहोस्; ! लाई केवल तब प्रयोग गर्नुहोस् जब तपाईं साँच्चै non-nullness प्रमाणित गर्न सक्नुहुन्छ।