 ```ts
export function topologicalSort(nodes: readonly string[], edges: readonly (readonly [string, string])[]): string[] {
    if (!Array.isArray(nodes) || !Array.isArray(edges)) {
        throw new Error("Invalid input");
    }

    const nodeSet = new Set(nodes);
    for (const edge of edges) {
        if (!nodeSet.has(edge[0]) || !nodeSet.has(edge[1])) {
            throw new Error("Edge contains a node not in nodes");
        }
    }

    const adjacencyList: Map<string, string[]> = new Map();
    const inDegree: Record<string, number> = {};

    for (const node of nodes) {
        adjacencyList.set(node, []);
        inDegree[node] = 0;
    }

    for (const [from, to] of edges) {
        adjacencyList.get(from)!.push(to);
        inDegree[to]++;
    }

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

    const sortedOrder: string[] = [];
    while (queue.length > 0) {
        const currentNode = queue.shift()!;
        sortedOrder.push(currentNode);

        for (const neighbor of adjacencyList.get(currentNode)!) {
            inDegree[neighbor]--;
            if (inDegree[neighbor] === 0) {
                queue.push(neighbor);
            }
        }
    }

    if (sortedOrder.length !== nodes.length) {
        throw new Error("Cycle detected");
    }

    return sortedOrder;
}
```