```ts
/** The index of target in a sorted array, or -1. */
export function binarySearch(items: readonly number[], target: number): number {
  let low = 0;
  let high = items.length; // Adjusted upper bound to include the end element
  while (low < high) {
    const mid = Math.floor((low + high) / 2);
    if (items[mid] === target) {
      return mid;
    }
    if (items[mid] < target) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  if (low === items.length && items[low] !== target) {
    return -1; // Return -1 if the target is not found at the last position
  }
  return low;
}
```