from typing import Union


def duplicate(nested_list: Union[list, int]) -> list:
    """Return a new nested list with all numbers in <nested_list> duplicated.

    Each integer in <nested_list> should appear twice *consecutively* in the
    output nested list. The nesting structure is the same as the input,
    only with some new numbers added. See doctest examples for details.

    If <nested_list> is an int, return a list containing two copies of it.

    >>> duplicate(1)
    [1, 1]
    >>> duplicate([])
    []
    >>> duplicate([1, 2])
    [1, 1, 2, 2]
    >>> duplicate([1, [2, 3]])  # NOT [1, 1, [2, 2, 3, 3], [2, 2, 3, 3]]
    [1, 1, [2, 2, 3, 3]]
    """
    new_list = []
    # base case:
    if isinstance(nested_list, int):
        new_list.extend([nested_list, nested_list])
    else:
        # nested_list is a list.
        for item in nested_list:
            # item is either an int or a list.
            if isinstance(item, int):
                new_list.extend(duplicate(item))
            else:
                # item is another list.
                new_list.append(duplicate(item))
    return new_list

    # try it with listcomp and ternary operators.. challenging!