microsoft / microsoft/TypeScript
keyof on generics arbitrarily returns `string` rather than the inferred values depending on the generic
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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).
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Öffne den verlinkten TypeScript Playground und reproduziere den Fehler mit genericFunction, BaseSpec und den unterschiedlichen args-Objekten. Untersuche, warum keyof Spec in diesem Fall generischer Inferenz zu string erweitert wird, und überprüfe anschließend, dass das Beispiel typgeprüft wird und das inferierte Ergebnis "someKey" ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100