```typescript
export function groupByLength(words: readonly string[]): Map<number, ReadonlyArray<string>> {
  const groups = new Map();
  for (const word of words) {
    let bucket = groups.get(word.length);
    if (bucket === undefined) {
      groups.set(word.length, [word]);
    } else {
      bucket.push(word);
    }
    if (!Array.isArray(bucket)) {
      throw Error(`Expected value is array but got ${typeof word}.`);
    }
  }
  return groups;
}
```