def _get_opt_vars(cr, case_names, var_type=None):
    """
    Return names of vars that are part of the optimization variables in at least one of the cases.

    Parameters
    ----------
    cr : CaseReader
        The CaseReader housing the data.
    case_names : Iterable of str
        The case_names from which the outputs with available residuals out are to be returned.
    var_type : None or str
        One of 'desvars', 'constraints', 'objectives', or None.

    Returns
    -------
    list
        A list of the variables with available residuals in at least one of the given cases.
    """
    vars = set()
    for case_name in case_names:
        case = cr.get_case(case_name)
        if var_type in ('desvars', None):
            vars |= case.get_design_vars().keys()
        if var_type in ('constraints', None):
            vars |= case.get_constraints().keys()
        if var_type in ('objectives', None):
            vars |= case.get_objectives().keys()
    return sorted(list(vars))