```ts
export function romanToInt(input: string): number {
  if (typeof input !== 'string') {
    throw new Error('Input must be a string');
  }
  
  if (input.length === 0) {
    throw new Error('Input cannot be empty');
  }
  
  const romanValues = new Map<string, number>([
    ['I', 1],
    ['V', 5],
    ['X', 10],
    ['L', 50],
    ['C', 100],
    ['D', 500],
    ['M', 1000]
  ]);
  
  const validChars = new Set<string>(['I', 'V', 'X', 'L', 'C', 'D', 'M']);
  
  // Validate all characters are valid
  for (const char of input) {
    if (!validChars.has(char)) {
      throw new Error('Invalid character in input');
    }
  }
  
  // Parse the roman numeral
  let result = 0;
  let i = 0;
  
  while (i < input.length) {
    const currentChar = input[i];
    const currentValue = romanValues.get(currentChar)!;
    
    // Check for subtractive pairs
    if (i + 1 < input.length) {
      const nextChar = input[i + 1];
      const nextValue = romanValues.get(nextChar)!;
      
      if (currentValue < nextValue) {
        result += nextValue - currentValue;
        i += 2;
        continue;
      }
    }
    
    result += currentValue;
    i += 1;
  }
  
  // Re-encode the result canonically
  const canonical = numberToRoman(result);
  
  if (canonical !== input) {
    throw new Error('Input is not in canonical form');
  }
  
  return result;
}

function numberToRoman(num: number): string {
  if (num <= 0 || num > 3999) {
    throw new Error('Number out of range');
  }
  
  const romanNumerals = [
    [1000, 'M'],
    [900, 'CM'],
    [500, 'D'],
    [400, 'CD'],
    [100, 'C'],
    [90, 'XC'],
    [50, 'L'],
    [40, 'XL'],
    [10, 'X'],
    [9, 'IX'],
    [5, 'V'],
    [4, 'IV'],
    [1, 'I']
  ];
  
  let result = '';
  
  for (const [value, numeral] of romanNumerals) {
    while (num >= value) {
      result += numeral;
      num -= value;
    }
  }
  
  return result;
}
```