Skip to content
DeveloperMemos

Filter nulls from a Typescript Array

TypeScript, Programming1 min read

When working with arrays in TypeScript, it's common to encounter scenarios where you need to remove null or undefined elements. This can be crucial for ensuring data consistency and preventing runtime errors. In this article, we'll explore various techniques to filter out nulls from a TypeScript array, providing clear examples along the way.

Understanding the Problem

Before we dive into the solutions, let's understand why filtering null values from an array is important. When dealing with arrays that may contain nullable elements, leaving nulls unchecked can lead to unexpected behavior and potential runtime errors. To ensure the reliability of our code, it's essential to clean up these nulls as part of our data processing tasks.

Using Array.prototype.filter()

One of the most straightforward approaches to removing nulls from a TypeScript array is to use the filter method provided by JavaScript arrays. This method creates a new array containing all elements for which the provided filtering function returns true.

1const arrayWithNulls: (number | null)[] = [1, 2, null, 4, null, 6];
2const filteredArray = arrayWithNulls.filter((element) => element !== null);
3console.log(filteredArray); // Output: [1, 2, 4, 6]

In this example, we have an array arrayWithNulls containing a mix of numbers and nulls. By using the filter method and providing a function that checks for non-null elements, we obtain a new array filteredArray without any null values.

Using Type Assertion

Another approach to filtering nulls involves using type assertion to narrow down the type of the array elements. By asserting the type to exclude null or undefined, we effectively create a new array that contains only non-null or non-undefined elements.

1const arrayWithNulls: (number | null)[] = [1, 2, null, 4, null, 6];
2const filteredArray = arrayWithNulls.filter((element): element is number => element !== null);
3console.log(filteredArray); // Output: [1, 2, 4, 6]

By incorporating a type assertion in the filtering function, we instruct TypeScript to treat the elements as numbers, excluding null values from the resulting array.

Using the NonNullable Utility Type

TypeScript provides utility types that can be used to manipulate types within your code. The NonNullable type is particularly useful when dealing with arrays containing potentially nullable values. By applying this utility type, we can effectively exclude null and undefined from the type of the array.

1const arrayWithNulls: (number | null)[] = [1, 2, null, 4, null, 6];
2const filteredArray: number[] = arrayWithNulls.filter((element): element is NonNullable<typeof element> => element !== null) as number[];
3console.log(filteredArray); // Output: [1, 2, 4, 6]

In this example, we use the NonNullable type to explicitly declare that the resulting array should only contain non-null elements - providing a clean and concise solution to the problem.


Filtering null values from a TypeScript array is an essential task when working with potentially nullable data. By leveraging methods such as filter, type assertion, and utility types like NonNullable, you can clean up your arrays and minimize the risk of runtime errors.