postfix ! ਕੰਪਾਇਲਰ ਨੂੰ ਦੱਸਦਾ ਹੈ "ਮੈਂ ਗ੍ਰੁਤੀ ਦਿੱਤੀ ਹੈ ਕਿ ਇਹ ਮੁੱਲ ਇੱਥੇ null ਜਾਂ undefined ਨਹੀਂ ਹੈ" — ਕਿਸੇ ਵੀ runtime check ਤੋਂ ਬਿਨਾ null/undefined ਨੂੰ ਇਸਦੀ type ਤੋਂ ਹਟਾਉਂਦਾ ਹੈ।
() {
.(name!.());
}
postfix ! ਕੰਪਾਇਲਰ ਨੂੰ ਦੱਸਦਾ ਹੈ "ਮੈਂ ਗ੍ਰੁਤੀ ਦਿੱਤੀ ਹੈ ਕਿ ਇਹ ਮੁੱਲ ਇੱਥੇ null ਜਾਂ undefined ਨਹੀਂ ਹੈ" — ਕਿਸੇ ਵੀ runtime check ਤੋਂ ਬਿਨਾ null/undefined ਨੂੰ ਇਸਦੀ type ਤੋਂ ਹਟਾਉਂਦਾ ਹੈ।
() {
.(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
ਇਹਨਾਂ ਵਿੱਚੋਂ ਹਰੇਕ ਦੀ ਗੈਰ-ਹਾਜ਼ਰੀ ਕੇਸ ਨੂੰ ਸੰਭਾਲਦਾ ਹੈ ਬਜਾਏ ਇਸ ਨੂੰ assert ਕਰਨ ਦੇ।
! ਇੱਕ ਤਿੱਖਾ ਸਾਧਨ ਹੈ: ਇਹ null-safety ਨੂੰ ਚੁੱਪ ਕਰਦਾ ਹੈ ਜਿਸ ਨੂੰ ਕੰਪਾਇਲਰ ਤੁਹਾਨੂੰ ਦੇਣਾ ਚਾਹ ਰਿਹਾ ਹੈ।
ਇਹ ਕਦੋਂ-ਕਦਾਈਂ ਜਾਇਜ਼ ਹੈ (ਤੁਹਾਡੇ ਕੋਲ ਅਜਿਹਾ ਗਿਆਨ ਹੈ ਜਿਸ ਬਾਰੇ ਕੰਪਾਇਲਰ ਨੂੰ ਪਤਾ ਨਹੀਂ), ਪਰ ਇਸਦੀ ਵਧੀਕ ਵਰਤੋਂ ਉਸੇ null-crash bugs ਨੂੰ ਦੁਬਾਰਾ ਪੇਸ਼ ਕਰਦੀ ਹੈ ਜੋ strictNullChecks ਰੋਕਦਾ ਹੈ।
?., ??, ਜਾਂ ਸਪੱਸ਼ਟ guard ਲਈ ਪਹਿਲਾਂ ਪਹੁੰਚੋ; ! ਦੀ ਵਰਤੋਂ ਸਿਰਫ਼ ਉਦੋਂ ਕਰੋ ਜਦੋਂ ਤੁਸੀਂ ਸੱਚਮੁੱਚ non-nullness ਨੂੰ ਸਾਬਤ ਕਰ ਸਕੋ।