Whetstone.
Nullable reference typesThe null-forgiving ! and when it's honest
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.

Reveal answer

Defensible: a LINQ Where(x => x.DecidedAtUtc != null) guarantees the value is non-null on every returned row, so x.DecidedAtUtc!.Value in the projection forgives a warning the compiler can't otherwise resolve. Objectionable: sprinkling ! on a value with no prior guard just to stop the build complaining, which turns a compile-time check into a runtime NullReferenceException.

Sign in to track your progress →