Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omitted arguments are not candidates for possibly-undefined type parameters #61069

Open
DanielRosenwasser opened this issue Jan 29, 2025 · 0 comments
Assignees
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 29, 2025

Background

Playground Link

Consider the following code that is trying to leverage #56941.

type QuickPickReturn<Multiple extends boolean | undefined> =
    Multiple extends true ? string[] :
    Multiple extends false | undefined ? string :
    never;

/**
 * @param prompt The text to show to a user.
 * @param items Whether a user can select multiple options, or just a single option.
 * @param canSelectMultiple Each of the options presented to the user.
 **/
function showQuickPick<Multiple extends boolean | undefined>(
    prompt: string,
    items: readonly string[],
    canSelectMultiple?: Multiple,
): QuickPickReturn<Multiple> {
    let buttons = items.map((item, index) => ({
        selected: index === 0,
        text: item,
    }));

    // other code goes code here...

    if (canSelectMultiple) {
        return buttons
            .filter(button => button.selected)
            .map(button => button.text);
    }
    return buttons.find(button => button.selected)!.text;
}

This code has an optional parameter canSelectMultiple. Because it is optional, the Multiple type parameters cannot simply be constrained to boolean - they must factor in undefined for various narrowing and assignability checks to work out.

Most calls work as expected.

// `true` gives a `string[]` - works ✅
let shoppingList: string[] = showQuickPick(
    "Which fruits do you want to purchase?",
    ["apples", "oranges", "bananas", "durian"],
    true,
);

// `false` - works ✅
let dinner: string = showQuickPick(
    "What's for dinner tonight?",
    ["sushi", "pasta", "tacos", "ugh I'm too hungry to think, whatever you want"],
    false,
);

undefined explicitly also works

// `undefined` gives a `string` - works ✅
let dinner2: string = showQuickPick(
    "What's for dinner tonight?",
    ["sushi", "pasta", "tacos", "ugh I'm too hungry to think, whatever you want"],
    undefined,
);

However, if we omit the argument, we assume that there are no candidates for inference available and fall back to the constraint boolean | undefined. This changes our output type to string | string[], which is an error in the following code:

// Omitted argument gives a `string | string[]` - ❌
let dinner3: string = showQuickPick(
    "What's for dinner tonight?",
    ["sushi", "pasta", "tacos", "ugh I'm too hungry to think, whatever you want"],
);

Proposal

When TypeScript infers type arguments to type parameters based on actual call arguments, we walk through parameters with missing corresponding arguments and infer from the type undefined to each parameter's type.

Some things to consider if we discuss this:

  • How would this function with missing properties? Do we need to do something similar there?
  • Is it always desirable to do this inference? Do we need a different inference priority?
  • This behavior is correct for purely-optional parameters, but is it correct for parameters with default arguments? (I realized this hairball of a problem after posting this issue)
@gabritto gabritto self-assigned this Jan 31, 2025
@gabritto gabritto added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

2 participants