```typescript
/**
 * Encodes a string by replacing each maximal run of one repeated character with that character followed by the run length.
 */
export function runLengthEncode(input: string): string {
    if (typeof input !== 'string') {
        throw new Error('Input must be a string');
    }

    const result = [];
    let currentChar = '';
    let currentCount = 0;

    for (const char of input) {
        if (char === currentChar) {
            currentCount++;
        } else {
            if (currentChar) {
                result.push(currentChar + (currentCount > 1 ? `${currentCount}` : '1'));
            }
            currentChar = char;
            currentCount = 1;
        }
    }

    if (currentChar) {
        result.push(currentChar + (currentCount > 1 ? `${currentCount}` : '1'));
    }

    return result.join('');
}
```