```ts
export function shelfFit(widths: number[], shelf: number): number {
  if (!Array.isArray(widths) || !Number.isInteger(shelf) || shelf < 0) {
    throw new Error('Invalid input');
  }

  let totalWidth = 0;
  for (const width of widths) {
    if (!Number.isInteger(width) || width <= 0) {
      throw new Error('Invalid input');
    }
    if (totalWidth + width > shelf) {
      break;
    }
    totalWidth += width;
  }

  return widths.length - (widths.length - Math.floor(totalWidth / shelf));
}
```