This utility type is useful for functions that can accept either a single value or an array of values, providing a more flexible API. The implementation can normalize the input to always work with arrays internally.
// Function that accepts either a single ID or multiple IDs
function deleteItems(ids: MaybeArray<string>): void {
// Normalize to array
const idArray = Array.isArray(ids) ? ids : [ids];
idArray.forEach(id => console.log(`Deleting ${id}`));
}
// Both calls are valid:
deleteItems('item-1'); // Single item
deleteItems(['item-1', 'item-2', 'item-3']); // Multiple items
// Common use case in permissions
interface GrantPermissionsParams {
permissions: MaybeArray<Permission>;
}
Creates a type that accepts either T or an array of T