```ts
/** Encode a string using run-length encoding. */
export function runLengthEncode(input: string): string {
  if (typeof input !== 'string') throw new Error('input must be a string');
  if (input.length === 0) return '';
  let result = '';
  let currentChar = input[0];
  let count = 1;
  for (let i = 1; i < input.length; i++) {
    const ch = input[i];
    if (ch === currentChar) {
      count++;
    } else {
      result += currentChar + count.toString();
      currentChar = ch;
      count = 1;
    }
  }
  result += currentChar + count.toString();
  return result;
}
```