```typescript
export function topologicalSort(nodes: readonly string[], edges: readonly (readonly [string, string])[]): string[] {
    if (!Array.isArray(edges)) return [];

    const graph = new Map<string, Set<string>>();
    const indegree = Array.from({ length: nodes.length }, () => 0);

    for (const [from, to] of edges) {
        if (!nodes.includes(from) || !nodes.includes(to)) {
            throw new Error('Invalid edge');
        }

        const fromIndex = nodes.indexOf(from);
        const toIndex = nodes.indexOf(to);

        graph.set(from, graph.get(from) || new Set<string>());
        graph.get(from)?.add(to);
        indegree[toIndex]++;
    }

    const queue: string[] = [];
    for (const [node, count] of indegree.entries()) {
        if (count === 0) {
            queue.push(node);
        }
    }

    const result: string[] = [];

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

        graph.get(node)?.forEach((neighbor) => {
            indegree[nodes.indexOf(neighbor)]--;
            if (indegree[nodes.indexOf(neighbor)] === 0) {
                queue.push(neighbor);
            }
        });
    }

    return nodes.length === result.length ? result : nodes;
}
```