```typescript
export function topologicalSort(nodes: readonly string[], edges: readonly (readonly [string, string])[]): string[] {
    if (!nodes.length) return [];
    
    const graph = new Map<string, Set<string>>();
    for (const [from, to] of edges) {
        if (!graph.has(from)) graph.set(from, new Set());
        if (!graph.has(to)) graph.set(to, new Set());
        
        graph.get(from)!.add(to);
    }

    const inDegree = new Map<string, number>();
    for (const node of nodes) {
        inDegree.set(node, 0);
    }
    
    for (const [from] of edges) {
        if (!inDegree.has(from)) throw new Error(`Node ${from} is not present in the nodes`);
        inDegree.set(from, inDegree.get(from)! + 1);
    }

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

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

        if (!graph.has(node)) continue;

        for (const neighbor of graph.get(node)!) {
            inDegree.set(neighbor, inDegree.get(neighbor)! - 1);
            if (inDegree.get(neighbor)! === 0) queue.push(neighbor);
        }
    }

    const remainingNodes = Array.from(inDegree.keys()).filter(node => inDegree.get(node)! > 0);
    if (remainingNodes.length > 0) {
        throw new Error(`Cycle detected or some nodes are not reachable: ${remainingNodes.join(', ')}`);
    }

    return result;
}
```