This is a common requirement in many applications where you need to ensure that an array has at least one element in TypeScript. There, you can use non-empty arrays in TypeScript. In this tutorial, I will explain how to define and use non-empty arrays in TypeScript.
Non-Empty Arrays in TypeScript
When working with arrays in TypeScript, it’s often important to ensure that an array is not empty. For instance, consider a scenario where you are working on a customer management system for a US-based retail company. You might need to ensure that the list of customer orders is never empty before processing it. An empty array could lead to unexpected behavior or errors in your application.
Define Non-Empty Arrays in TypeScript
To define a non-empty array in TypeScript, you can use tuple types and variadic tuple types. This approach allows you to enforce that an array has at least one element. Here’s how you can define a non-empty array type:
type NonEmptyArray<T> = [T, ...T[]];This type definition ensures that the array has at least one element of type T. The first element is mandatory, and the rest are optional.
Check out Remove Duplicates from an Array in TypeScript
Example Of Non-Empty Arrays in TypeScript: Customer Orders
Let’s say you are developing a system for a retail company in New York, and you need to handle customer orders. You want to ensure that the list of orders is never empty before processing. Here’s how you can define and use a non-empty array type for this scenario:
type NonEmptyArray<T> = [T, ...T[]];
interface Order {
id: number;
product: string;
quantity: number;
}
const processOrders = (orders: NonEmptyArray<Order>): void => {
orders.forEach(order => {
console.log(`Processing order ${order.id} for ${order.product}`);
});
};
const customerOrders: NonEmptyArray<Order> = [
{ id: 1, product: 'Laptop', quantity: 2 },
{ id: 2, product: 'Smartphone', quantity: 1 },
];
processOrders(customerOrders);In this example, the processOrders function expects a NonEmptyArray of Order objects. This ensures that the function always receives a non-empty array, preventing potential errors from processing an empty list.
Here is the exact output you can see in the screenshot below:

Read Sort String Arrays in TypeScript
Benefits of Using Non-Empty Arrays
Using non-empty arrays in TypeScript provides several benefits:
- Type Safety: It ensures that your functions receive valid data, reducing the risk of runtime errors.
- Readability: It makes your code more readable and self-documenting, indicating that an array should never be empty.
- Maintainability: It helps maintain the integrity of your data structures, making your codebase easier to manage and debug.
Advanced Example: Product Inventory
Let me show you another advanced example of a TypeScript non-empty array.
Consider another example where you manage the inventory for a chain of stores across the USA. You need to ensure that the list of products in each store is never empty. Here’s how you can achieve this using non-empty arrays:
type NonEmptyArray<T> = [T, ...T[]];
interface Product {
id: number;
name: string;
price: number;
stock: number;
}
const updateInventory = (storeId: number, products: NonEmptyArray<Product>): void => {
console.log(`Updating inventory for store ${storeId}`);
products.forEach(product => {
console.log(`Product: ${product.name}, Stock: ${product.stock}`);
});
};
const storeProducts: NonEmptyArray<Product> = [
{ id: 101, name: 'TV', price: 499.99, stock: 10 },
{ id: 102, name: 'Refrigerator', price: 899.99, stock: 5 },
];
updateInventory(1, storeProducts);In this example, the updateInventory function ensures that the products array is never empty, providing a robust way to manage store inventories.
Read How to Use the TypeScript forEach Method on Arrays?
Handle Non-Empty Arrays with Type Guards in TypeScript
Sometimes, you might need to validate that an array is non-empty at runtime. You can achieve this using type guards in TypeScript. Here’s an example:
function isNonEmptyArray<T>(arr: T[]): arr is NonEmptyArray<T> {
return arr.length > 0;
}
const orders: Order[] = [];
if (isNonEmptyArray(orders)) {
processOrders(orders);
} else {
console.log('No orders to process.');
}In this example, the isNonEmptyArray function checks if the array is non-empty and narrows its type accordingly. This ensures that the processOrders function only gets called with a non-empty array.
Conclusion
Defining and using non-empty arrays in TypeScript is required to enforce data integrity and prevent runtime errors in your applications. By using tuple types and type guards, you can ensure that your functions always receive valid, non-empty arrays. This approach not only enhances type safety but also improves the readability and maintainability of your code.
In this tutorial, I explained how to define and use non-empty arrays in TypeScript using different methods and examples.
You may also like:
- Convert TypeScript Enums to Arrays
- TypeScript Const Arrays
- Convert an Enum to an Array in TypeScript

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.