microsoft / microsoft/TypeScript

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

Aperta
#41,645 6 commenti 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: LS: Completion Lists Effort: Moderate Experience Enhancement Experimentation Needed Help Wanted Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

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.

Direzione di ricerca

Inizia riproducendo l’esempio TypeScript 4.1.2 dell’issue, concentrandoti sull’inferenza dall’oggetto quantities all’array allergens e sui relativi risultati del completamento automatico. Confronta il comportamento riportato con l’ordine inverso e considera conclusa l’issue quando "eggs" e "flour" vengono proposti come completamenti per allergens senza modificare i tipi generici dell’esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.