# How to Effectively Use Type Predicates in TypeScript

```ts
(v): v is number => v !== null && v !== undefined
```

---

### 🧠 What Is a Type Predicate?

A **type predicate** is a way to tell TypeScript:

> “If this condition is true, then you can safely treat `v` as a specific type.”

The syntax is:

```ts
(parameter): parameter is Type => condition
```

So in your case:

* `v` is the parameter
    
* `v is number` is the type predicate
    
* `v !== null && v !== undefined` is the condition
    

---

### 🔍 What It Does

This function is used inside `.filter()` to **remove** `null` and `undefined`, and **tell TypeScript** that the remaining values are definitely `number`.

#### Without Type Predicate:

```ts
const arr = [1, null, 2, undefined];

const filtered = arr.filter(v => v !== null && v !== undefined);
// TypeScript still thinks: (number | null | undefined)[]
```

#### With Type Predicate:

```ts
const filtered = arr.filter(
  (v): v is number => v !== null && v !== undefined
);
// TypeScript now knows: number[]
```

✅ You can now safely use `.toLocaleString()` or other number methods without type errors.

---

### 🧪 Real Example

```ts
const salaries = [50000, null, undefined, 75000];

const cleanSalaries = salaries
  .filter((v): v is number => v !== null && v !== undefined)
  .map(s => `$${s.toLocaleString()}`);
```

Without the type predicate, TypeScript would complain about `s.toLocaleString()` because `s` might still be `null` or `undefined`.

---

### 🧠 TL;DR

| Expression | What It Does |
| --- | --- |
| `(v): v is number => ...` | TypeScript knows `v` is a number |
| `v => v !== null && v !== undefined` | Works at runtime, but TS still unsure |
| `v => Boolean(v)` | Removes all falsy values (including `0`) |

---
