```ts
export function expandNthWeekday(ordinal: number, weekday: number, start: string, months: number): string[] {
    if (![-1, 1, 2, 3, 4, 5].includes(ordinal)) throw new Error("Invalid ordinal");
    if (weekday < 0 || weekday > 6) throw new Error("Invalid weekday");
    const startMatch = /^(\d{4})-(\d{2})$/.exec(start);
    if (!startMatch) throw new Error("Invalid start date format");
    const [year, month] = startMatch.slice(1).map(Number);
    if (year < 1 || year > 9999) throw new Error("Year out of range");
    if (month < 1 || month > 12) throw new Error("Month out of range");
    if (months <= 0 || months > 240) throw new Error("Invalid number of months");

    const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    function isLeapYear(year: number): boolean {
        return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
    }

    const results: string[] = [];

    for (let i = 0; i < months; i++) {
        const currentYear = Math.floor((year * 12 + month - 1 + i) / 12);
        const currentMonth = ((year * 12 + month - 1 + i) % 12) + 1;
        if (currentYear > 9999) break;

        if (isLeapYear(currentYear)) daysInMonth[1] = 29;
        else daysInMonth[1] = 28;

        const firstDayOfWeek = (3 + (currentYear - 1754) * 365 + Math.floor((currentYear - 1754) / 4) - Math.floor((currentYear - 1754) / 100) + Math.floor((currentYear - 1754) / 400) + ((currentMonth - 1) * 31 + (Math.floor((currentMonth - 8) / 2) % 2) * 30 + (currentMonth === 3 || currentMonth === 5 || currentMonth === 8 || currentMonth === 10 ? 0 : 29)) - 1) % 7;

        const dayOccurrences: number[] = [];
        for (let day = 1; day <= daysInMonth[currentMonth - 1]; day++) {
            if ((firstDayOfWeek + (day - 1)) % 7 === weekday) {
                dayOccurrences.push(day);
            }
        }

        let selectedDay: number | null = null;
        if (ordinal > 0 && ordinal <= dayOccurrences.length) {
            selectedDay = dayOccurrences[ordinal - 1];
        } else if (ordinal === -1 && dayOccurrences.length > 0) {
            selectedDay = dayOccurrences[dayOccurrences.length - 1];
        }

        if (selectedDay !== null) {
            const paddedMonth = currentMonth.toString().padStart(2, '0');
            const paddedDay = selectedDay.toString().padStart(2, '0');
            results