Nullable reference typesThe null-forgiving ! and when it's honest
No narration yet
Module 4 · Lesson 29 min

The null-forgiving ! and when it's honest

The ! operator is the null-forgiving operator: x! tells the compiler "I know this looks nullable, trust me, it isn't." It's TS's ! non-null assertion, and it carries the same risk: you're overriding the checker, so if you're wrong it's a NullReferenceException at runtime instead of a compile error. Use it only when you genuinely know more than the compiler.

An honest use of ! from the real query store
// The WHERE clause guarantees DecidedAtUtc != null for every row returned,
// so the compiler's nullable warning here is a false positive we forgive.
DecidedAtUtc: x.DecidedAtUtc!.Value,
DecisionTypeCode: x.DecisionTypeCode!,
Practice

Try it yourself

Recall

When ! is defensible

Name the condition, not just the key.

Give a concrete case where x! is defensible in Fusion code, and one where it would draw a reviewer's objection.