```ts
export function topologicalSort(nodes: readonly string[], edges: readonly (readonly [string, string])[]): string[] {
  const graph: { [node: string]: string[] } = {};
  const inDegree: { [node: string]: number } = {};

  for (const node of nodes) {
    graph[node] = [];
    inDegree[node] = 0;
  }

  for (const [from, to] of edges) {
    if (!nodes.includes(from)) throw new Error(`Edge has node "${from}" not in nodes`);
    if (!nodes.includes(to)) throw new Error(`Edge has node "${to}" not in nodes`);
    graph[from].push(to);
    inDegree[to]++;
  }

  const queue: string[] = [];
  for (const node of nodes) {
    if (inDegree[node] === 0) queue.push(node);
  }

  const result: string[] = [];
  while (queue.length > 0) {
    if (queue.length > 1) throw new Error("Graph contains a cycle");
    const current = queue.shift()!;
    result.push(current);
    for (const neighbor of graph[current]) {
      inDegree[neighbor]--;
      if (inDegree[neighbor] === 0) queue.push(neighbor);
    }
  }

  if (result.length !== nodes.length) throw new Error("Graph contains a cycle");
  return result;
}
```