```typescript
export function maxSlidingWindow(nums: number[], k: number): number[] {
  if (!Number.isInteger(k) || k < 1 || k > nums.length) {
    throw new Error("k must be an integer between 1 and the length of nums");
  }

  const result: number[] = [];
  const deque: number[] = [];

  for (let i = 0; i < nums.length; i++) {
    // Remove indices that are out of the current window
    while (deque.length > 0 && deque[0] <= i - k) {
      deque.shift();
    }

    // Remove indices whose corresponding values are less than the current number
    while (deque.length > 0 && nums[deque[deque.length - 1]] < nums[i]) {
      deque.pop();
    }

    // Add the current index to the deque
    deque.push(i);

    // The first element in the deque is the maximum of the current window
    if (i >= k - 1) {
      result.push(nums[deque[0]]);
    }
  }

  return result;
}
```