```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 romanMap = new Map<string, number>([
    ['I', 1],
    ['V', 5],
    ['X', 10],
    ['L', 50],
    ['C', 100],
    ['D', 500],
    ['M', 1000]
  ]);
  
  for (const char of input) {
    if (!romanMap.has(char)) {
      throw new Error('Invalid character in input');
    }
  }
  
  let value = 0;
  let i = 0;
  
  while (i < input.length) {
    const currentChar = input[i];
    const currentValue = romanMap.get(currentChar)!;
    
    if (i + 1 < input.length) {
      const nextChar = input[i + 1];
      const nextValue = romanMap.get(nextChar)!;
      
      if (currentValue < nextValue) {
        value += nextValue - currentValue;
        i += 2;
        continue;
      }
    }
    
    value += currentValue;
    i += 1;
  }
  
  if (value <= 0 || value > 3999) {
    throw new Error('Number out of range');
  }
  
  // Re-encode the value canonically and compare with input
  const canonical = numberToRoman(value);
  
  if (canonical !== input) {
    throw new Error('Input is not canonical');
  }
  
  return value;
}

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;
}
```