# Null Safety

> A type rule. A value can be empty only if its type says so.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** Null Safe, Strict Null Checks, Optional Type

## Think of it like

Think of it like a gift box. The label must say if it might be empty.

## Definition

Null safety is a type rule that stops a value from being empty unless you say it may be. In a null-safe language, a string is always a real string, never null. If a value might be missing, its type says so, and you must check before using it. That removes a whole class of crash where code reads a value that is not there. Kotlin, Swift, Rust and TypeScript in strict mode all work this way.

## Example

Jack sets a name of type string to null. TypeScript refuses before the app runs, so the crash never reaches a user.

## Example code

```typescript
let name: string = null;
// Type 'null' is not assignable to type 'string'.
```

In strict mode, a string may not be empty by accident.

## Why it matters

Reading a value that is not there is one of the commonest crashes in software. Null safety catches it before the code runs.

## Common mistake

Silencing the check with a ! or a cast to make the error go away. The crash then comes back at run time.

## Three principles

- **Never empty by default.** A string is a string.
- **Say when it may be.** The type shows it.
- **Check before use.** The checker insists.

## Related terms

- [Null](https://learn.benibix.com/term/development/null)
- [Type System](https://learn.benibix.com/term/development/type-system)
- [TypeScript](https://learn.benibix.com/term/development/typescript)
- [Data Type](https://learn.benibix.com/term/development/data-type)
- [Union Type](https://learn.benibix.com/term/development/union-type)

---

Source: https://learn.benibix.com/term/development/null-safety
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
