from typing import Any
from typing import Union


def to_number(s: Any) -> Union[int, float]:
    """Cast the string representation of the given object to a number (int or float), or raise ValueError."""
    try:
        return int(str(s))
    except ValueError:
        return float(str(s))