```ts
export function expandGlossary(script: string[]): string {
  if (!Array.isArray(script)) {
    throw new Error("Input script is not a list");
  }

  const slots: Record<string, string> = {};
  const sent: string[] = [];

  for (const line of script) {
    if (typeof line !== "string") {
      throw new Error("Line is not a string");
    }

    if (line.startsWith("!")) {
      const slotNumber = line.slice(1);
      if (!slotNumber.match(/^\d+$/)) {
        throw new Error("Invalid slot number in send line");
      }
      sent.push(slots[slotNumber] ?? "");
    } else if (line.includes("=")) {
      const cut = line.indexOf("=");
      const slotNumber = line.slice(0, cut);
      const body = line.slice(cut + 1);

      if (!slotNumber.match(/^\d+$/)) {
        throw new Error("Invalid slot number in store line");
      }
      if (slots[slotNumber] !== undefined) {
        throw new Error("Slot stored a second time");
      }

      slots[slotNumber] = expandBody(body, slots);
    } else {
      throw new Error("Line of neither kind");
    }
  }

  return sent.join("");
}

function expandBody(body: string, slots: Record<string, string>): string {
  let balance = 0;
  let expanded = "";

  for (let i = 0; i < body.length; i++) {
    const char = body[i];

    if (char === "{") {
      if (i + 1 < body.length && body[i + 1] === "{") {
        expanded += "{";
        i++;
      } else {
        balance++;
      }
    } else if (char === "}") {
      if (i > 0 && body[i - 1] === "}") {
        expanded += "}";
        i--;
      } else {
        balance--;
        if (balance < 0) {
          throw new Error("Closing brace with nothing open");
        }
      }
    } else if (char === "]" && balance === 0) {
      const match = body.slice(i).match(/^\{(\d+)\}/);
      if (match) {
        const slotNumber = match[1];
        expanded += slots[slotNumber] ?? "";
        i += match[0].length - 1;
      }
    } else {
      expanded += char;
    }
  }

  if (balance !== 0) {
    throw new Error("Brace opened and never closed");
  }

  return expanded;
}
```