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

    // Initialize the graph and in-degree map
    for (const node of nodes) {
        graph[node] = new Set();
        inDegree[node] = 0;
    }

    // Build the graph and calculate in-degrees
    for (const [from, to] of edges) {
        if (!graph[from] || !graph[to]) {
            throw new Error(`Edge references a node not in nodes: ${from} or ${to}`);
        }
        graph[from].add(to);
        inDegree[to]++;
    }

    const queue: string[] = [];
    // Add all nodes with zero in-degree to the queue
    for (const node of nodes) {
        if (inDegree[node] === 0) {
            queue.push(node);
        }
    }

    const sortedNodes: string[] = [];

    while (queue.length > 0) {
        const node = queue.shift()!;
        sortedNodes.push(node);

        // Decrease the in-degree of adjacent nodes
        for (const neighbor of graph[node]) {
            inDegree[neighbor]--;
            if (inDegree[neighbor] === 0) {
                queue.push(neighbor);
            }
        }
    }

    // If sortedNodes doesn't include all nodes, there's a cycle
    if (sortedNodes.length !== nodes.length) {
        throw new Error('Cycle detected in the graph');
    }

    return sortedNodes;
}
```