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

    nodes.forEach(node => {
        graph.set(node, new Set());
        inDegree.set(node, 0);
    });

    edges.forEach(([from, to]) => {
        if (!graph.has(from) || !graph.has(to)) {
            throw new Error(`Invalid edge: node ${from} or ${to} not in nodes`);
        }
        graph.get(from)?.add(to);
        inDegree.set(to, (inDegree.get(to) ?? 0) + 1);
    });

    const queue: string[] = [];
    nodes.forEach(node => {
        if ((inDegree.get(node) ?? 0) === 0) {
            queue.push(node);
        }
    });

    const result: string[] = [];

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

        graph.get(node)?.forEach(neighbor => {
            inDegree.set(neighbor, (inDegree.get(neighbor) ?? 0) - 1);
            if ((inDegree.get(neighbor) ?? 0) === 0) {
                queue.push(neighbor);
            }
        });
    }

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

    return result;
}
```