question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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[]

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
andrewbranchcommented, Nov 25, 2020

This is because the array is a stronger inference candidate than the record, so as soon as the array is non-empty, the type parameter gets instantiated to the types that can be inferred from its contents. The completions behavior you expect seems so obvious, but it’s actually really tricky to make it work. This is very similar to #36556 though, where we got reasonably good results, so maaaaybe that approach can be applied here.

0reactions
devanshjcommented, Jun 9, 2021

Ah my bad. Here’s another workaround. You do get an error that property sugar is missing (so the types are correct) but there’s no autocomplete, I suspect that’s because of #44428 bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typing an array of generic inferred types - Stack Overflow
I have a generic type to define the object in the array. When calling it to generate the array type, an error is...
Read more >
Creating a Generic Array in Java - Baeldung
The loose types of Java generics are hard to coerce into the strong types of Java arrays. We explore the problem and some...
Read more >
How To Simulate Generic Arrays In Java?
This Tutorial Explains How to Simulate the Functionality of Generic Array in Java using Object Array and also using Reflection Class with ...
Read more >
Inferred BRAM with initial value via generic (VHDL)
The problem I am facing is that I cannot declare a two dimensional array in the generics using the same values of the...
Read more >
An introduction to generics in Swift using its built-in types
The only condition being that the array is homogenous, in other words, it can only contain objects of a single type. So how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found