🧭Null Safety
Also known as Null Safe, Strict Null Checks, Optional Type
A type rule. A value can be empty only if its type says so.
Think of it like
Think of it like a gift box. The label must say if it might be empty.
Example
Jack sets a name of type string to null. TypeScript refuses before the app runs, so the crash never reaches a user.
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.
Definition
Example code
typescript
let name: string = null;
// Type 'null' is not assignable to type 'string'.Common mistake
Key takeaways
- 1Never empty by default. A string is a string.
- 2Say when it may be. The type shows it.
- 3Check before use. The checker insists.