> For the complete documentation index, see [llms.txt](https://katxupa.gitbook.io/katxupa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://katxupa.gitbook.io/katxupa/get-a-taste-of-katxupa/usage/null-safety.md).

# Null Safety

Null-safety refers to the language's features and practices that aim to prevent or mitigate the issues associated with *null* and *undefined* values. TypeScript has introduced stricter null-checking capabilities to enhance the overall safety and robustness of the code.

These [features](/katxupa/get-a-taste-of-katxupa/usage/null-safety/typescript-null-safety.md) 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.

**So, what can&#x20;*****Katxupa*****&#x20;provide on top of this?**

1. First of all, one big advantage of **Katxupa** is to bring functional programming paradigm through scope functions and type extensions. To keep the flow, you can make use of  [Optional Chaining Operator](https://github.com/tc39/proposal-optional-chaining) (*?.*) when using scope functions to ensure null-safety:

```typescript
// Only run the code block if "numberOrUndefined" is defined 
const numberOrUndefinedOrNull = await bitcoinService.getPrice();
numberOrUndefinedOrNull?.letIt((it) => {
    it++;
    it = it * 100;
    return it;
});

// Actually, there is no need to declare a variable
(await bitcoinService.getPrice())
    ?.letIt((it) => {
        it++;
        it = it * 100;
        return it;
    });// Some code
```

2. Secondly, dealing with *null* and *undefined* values can be improved by using the *Optional* concept. ***Optional gives the power to differentiate between the absence of a value and the null or undefined reference.*** S&#x6F;*, Optional* is a wrapper utility  that provides a way to handle potentially *null* or *undefined* values in a concise and expressive manner. It also allows various operations on the value, such as checking if it is present, retrieving it, applying transformations and handling empty values.

{% code lineNumbers="true" %}

```ts
// Example 1
// Get Usecase:
//    1- Validate if user exists
//    2- Through an HttpError if not 
//    3- Return the user object
return optionalOf(user)
    .orElseThrow(() => new NotFoundError("User doesn't exist"))
    .get();

// Example 2
// Delete Usecase:
//    1- Validate if user exists
//    2- Through an HttpError if not 
//    3-  Delete the user from the database asynchronously and await for the result
await Optional.of(user)
  .orElseThrow(() => new HttpError(409, "User doesn't exist"))
  .runAsync(() => this.userRepository.delete({id: userId}));

// Example 3
// Update usecase:
//    1- Validate if user exists
//    2- Through an HttpError if not
//    3- If exists, merge the existing one with additional userData
//    4- Call the user repository and save the updated user asyncronously, returning a promise
//            
return optionalOf(user)
    .orElseThrow(() => new HttpError(409, "User doesn't exist"))
    .map(user => {
        return {
            ...user,
            userData,
        };
    })
    .runAsync(user => this.userRepository.save(user));
```

{% endcode %}

**Please also have a look in the following links to have a dip dive into the topic.**

{% content-ref url="/pages/9CudMquK13AMxzACvwGC" %}
[Optional](/katxupa/get-a-taste-of-katxupa/dip-dive/optional.md)
{% endcontent-ref %}

{% content-ref url="/pages/c4UNjw3iH2yDGPS6YQSz" %}
[TypeScript Null-Safety](/katxupa/get-a-taste-of-katxupa/usage/null-safety/typescript-null-safety.md)
{% endcontent-ref %}

{% content-ref url="/spaces/mjbjQsEf3GbVFMNGZDB3/pages/FTL6iM7AebiiC0xahODH" %}
[Broken mention](broken://spaces/mjbjQsEf3GbVFMNGZDB3/pages/FTL6iM7AebiiC0xahODH)
{% endcontent-ref %}
