---
description: TypeScript null-safety overview
---

# TypeScript Null-Safety

Here are some key aspects of null-safety in TypeScript:

1.  **Strict Null Checks:** TypeScript introduced strict null checks, which means that by default, variables cannot be assigned the value of `null` or `undefined` unless explicitly specified. If you try to assign or use a variable that might be `null` or `undefined`, TypeScript will generate a compilation error.

    ```typescript
    // Without strict null checks (default behavior):
    let name: string = null; // No error

    // With strict null checks:
    let name: string = null; // Error: Type 'null' is not assignable to type 'string'.
    ```
2.  **Nullable and Non-Nullable Types:** TypeScript allows you to explicitly define whether a variable can be `null` or `undefined` by using the union type with `null` or `undefined`.

    <pre class="language-typescript"><code class="lang-typescript"><strong>let name: string | null = null; // Nullable type
    </strong>
    let age: number | undefined = undefined; // Nullable type

    let count: number = 5; // Non-nullable type
    </code></pre>
3.  **Optional Parameters and Properties:** You can use the `?` modifier for function parameters and object properties to indicate that they are optional and can be `undefined`.

    ```typescript
    function greet(name?: string) {
      // ...
    }

    interface Person {
      name?: string;
      age?: number;
    }
    ```
4.  **Non-Null Assertion Operator (`!`):** TypeScript provides the non-null assertion operator (`!`), which tells the compiler to consider a value as non-null even if TypeScript's type system can't guarantee it. This should be used with caution because it might lead to runtime errors if the value is, in fact, `null` or `undefined`.

    ```typescript
    let element: HTMLElement | null = document.getElementById('myElement');
    let value: string = element!.innerText; // Non-null assertion
    ```
5.  **Strict Property Initialisation:** When using the _`strictPropertyInitialization`_ flag, TypeScript ensures that class properties are initialised in the constructor or have a definite assignment assertion (`!`).

    ```typescript
    class Example {
      name!: string; // Definite assignment assertion
    }
    ```
6.  **Type Guard Functions:** Type guards are functions that help narrow down the type of a variable within a certain code block. For example, checking if a variable is not `null` before using it.

    ```typescript
    function isNonNull(value: string | null): value is string {
      return value !== null;
    }

    let input: string | null = // ...;
    if (isNonNull(input)) {
      // input is string here
    }
    ```

These features collectively contribute to a more robust null-safety mechanism in TypeScript, reducing the likelihood of null-related runtime errors. However, it's important for developers to understand and apply these features appropriately to ensure code correctness and safety.
