microsoft / microsoft/TypeScript

keyof on generics arbitrarily returns `string` rather than the inferred values depending on the generic

Aperta
#44,121 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Index Types
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Bug Report

🔎 Search Terms

keyof generic string

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about it
⏯ Playground Link

Playground link with relevant code

💻 Code
export type Item = {
  args?: Record<string, string>;
};

export type BaseSpec = Record<string, {
  items: Item[];
}>;

export function genericFunction<Spec extends BaseSpec>(spec: Spec): keyof Spec {
  return {} as any;
}

const AAAAA: `someKey` = genericFunction({
  someKey: {
    items: [{
      args: {},
    }, {
      args: {
        name: `foo`,
      },
    }],
  },
});
🙁 Actual behavior
Type 'string' is not assignable to type '"someKey"'.
🙂 Expected behavior

No errors

💻 Additional Information

Weirdly, it seems to work if all args objects that are specified have exactly the same content. Removing an arg object seems to work, and it works as well if we add name: "foo" into the first arg object (or remove it from the second one).

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

Apri il TypeScript Playground collegato e riproduci l’errore con genericFunction, BaseSpec e i diversi oggetti args. Analizza perché keyof Spec viene ampliato a string in questo caso di inferenza generica, quindi verifica che l’esempio superi il controllo dei tipi e che il risultato inferito sia "someKey".

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à
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.