Incorrect inference/autocompletion on generic arrays, when values can be inferred from a defined object.
See original GitHub issueTypeScript 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.
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 ofINGREDIENTS[]
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.