microsoft / microsoft/TypeScript

Omitted arguments are not candidates for possibly-undefined type parameters

Aperta
#61,069 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@gabritto ci sta già lavorando.

Dal 31/1/2025.

Domain: check: Type Inference Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.