Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Incorrect inference/autocompletion on generic arrays, when values can be inferred from a defined object. #41645

Copy link
Copy link
@TheMrZZ

Description

@TheMrZZ
Issue body actions

TypeScript Version: 4.1.2

Search Terms:
Autocompletion, incorrect, values, inference, generic, array, object, keys

Summary:
When an interface/a type has an object with generic keys, and an array of those keys, the array values cannot be infered from the object keys.

Code

interface Recipe<INGREDIENTS extends string> {
  quantities: Record<INGREDIENTS, number>
  allergens?: INGREDIENTS[]
}

function createRecipe<INGREDIENTS extends string>(recipe: Recipe<INGREDIENTS>) {}

createRecipe({
  quantities: {
    eggs: 1,
    flour: 2,
  },
  allergens: ['']
})

Expected behavior:
Here, when trying to give a value to allergens, the autocompletion should show "eggs" | "flour".

Actual behavior:
The autocompletion doesn't find anything.
image

Notes:
The other way is working: you can fill the array first, then the object keys will autocomplete - but this rarely make sense to write things that way.

Failed workarounds:
This bug is still present, even when:

  • allergens is optionnal
  • We switch from an interface to a type
  • We use keyof this['quantities'] instead of INGREDIENTS[]
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: LS: Completion ListsThe issue relates to showing completion lists in an editorThe issue relates to showing completion lists in an editorEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsExperimentation NeededSomeone needs to try this out to see what happensSomeone needs to try this out to see what happensHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.