from typing import List
from typing import Optional


def task_list(items: List[str], items_to_check: Optional[List[int]] = None) -> str:
    """Creates a task list in GitHub Flavored Markdown where each item can be checked off. This task list cannot be used
    inside a table.

    :param items: The items in the task list
    :param items_to_check: The indices (starting from 0) of the items that should be checked off

    :raises: Exception if an index in items_to_check does not exist in the items list
    """
    if not items_to_check:
        items_to_check = []

    for index in items_to_check:
        if index < 0 or index > len(items) - 1:
            raise Exception(f'Cannot check off non-existent index {index} in task list')

    task_list_syntax = ''

    for index, item in enumerate(items):
        task_list_syntax += f'- [{"x" if index in items_to_check else " "}] {item}\n'

    return task_list_syntax.rstrip('\n')