2024-04-25 14:56:13 +03:00
|
|
|
# isNotNil
|
|
|
|
|
|
|
|
Checks if the given value is not null nor undefined.
|
|
|
|
|
|
|
|
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to ensure it is not nullable.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
function isNotNil<T>(x: T | null | undefined): x is T;
|
|
|
|
```
|
|
|
|
|
2024-06-04 11:19:26 +03:00
|
|
|
### Parameters
|
2024-04-25 14:56:13 +03:00
|
|
|
|
|
|
|
- `x` (`T | null | undefined`): The value to test if it is not null nor undefined.
|
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
|
|
|
(`x is T`): True if the value is not null nor undefined, false otherwise.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
// Here the type of `arr` is (number | undefined)[]
|
|
|
|
const arr = [1, undefined, 3];
|
|
|
|
// Here the type of `result` is number[]
|
|
|
|
const result = arr.filter(isNotNil);
|
|
|
|
// result will be [1, 3]
|
2024-06-04 11:19:26 +03:00
|
|
|
```
|