```ts

export function mergeIntervals(intervals: number[][]): number[][] {
  if (intervals.length === 0) return [];

  // Validate intervals and sort by start
  const sortedIntervals = intervals.map(([start, end]) => {
    if (start > end) throw new Error("Invalid interval: start is greater than end");
    return [start, end];
  }).sort((a, b) => a[0] - b[0]);

  const merged: number[][] = [];
  let currentInterval = [...sortedIntervals[0]];

  for (let i = 1; i < sortedIntervals.length; i++) {
    const [nextStart, nextEnd] = sortedIntervals[i];

    if (currentInterval[1] >= nextStart) {
      // Intervals overlap or touch, merge them
      currentInterval[1] = Math.max(currentInterval[1], nextEnd);
    } else {
      // No overlap, add the current interval to merged and move to the next
      merged.push([...currentInterval]);
      currentInterval = [nextStart, nextEnd];
    }
  }

  // Add the last interval
  merged.push([...currentInterval]);

  return merged;
}

```