```ts
/**
 * Perform topological sort on given nodes and edges.
 * @param {string[]} nodes - Array of node names.
 * @param {readonly (readonly [string, string])[]} edges - Array of edges as tuples [from, to].
 * @returns {string[]} - Topologically sorted array of nodes.
 */
export function topologicalSort(nodes: readonly string[], edges: readonly (readonly [string, string])[]): string[] {
    const graph: Record<string, string[]> = {};
    const inDegree: Record<string, number> = {};
    const result: string[] = [];

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

    // Build the graph and calculate in-degrees
    edges.forEach(([from, to]) => {
        if (!nodes.includes(from) || !nodes.includes(to)) {
            throw new Error(`Node ${from} or ${to} not found in nodes`);
        }
        graph[from].push(to);
        inDegree[to]++;
    });

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

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

        for (const neighbor of graph[current]) {
            inDegree[neighbor]--;
            if (inDegree[neighbor] === 0) {
                queue.push(neighbor);
            }
        }
    }

    // Check for cycles
    if (result.length !== nodes.length) {
        throw new Error("Cycle detected");
    }

    return result;
}
```