```tsx
function runLengthEncode(input: string): string {
    if (typeof input !== 'string') throw new Error("Argument must be a string");

    let encoded = '';
    let currentChar: string;
    let count = 1;

    for (let i = 0; i < input.length; i++) {
        currentChar = input[i];

        // Edge case for boundary digits
        if (i === 0 || count >= 9) {
            if (i > 0 && encoded.slice(-1) === count.toString()) {
                encoded += currentChar;
            } else {
                encoded += currentChar + count;
            }
            count = 1;
        }

        // Edge case for duplicate values
        if (input[i] === currentChar) {
            count++;
        } else {
            if (count >= 2) {
                encoded += currentChar + count;
            } else {
                encoded += currentChar;
            }
            currentChar = input[i];
            count = 1;
        }
    }

    // Handle the edge case at the end of the string
    if (currentChar && count >= 9) encoded += currentChar + count;
    else if (currentChar && count === 2) encoded += currentChar + '1';
    else if (currentChar) encoded += currentChar;

    return encoded;
}
```