```ts
/** The words grouped by their length, in first-seen order. */
export function groupByLength(words: readonly string[]): Map<number, ReadonlyArray<string>> {
  const groups = new Map();
  for (const word of words) {
    if (typeof word !== 'string') throw new Error('Invalid argument: word must be a string');
    const bucket = groups.get(word.length) ?? [];
    groups.set(word.length, [...bucket, word]);
  }
  return groups;
}
```