microsoft / microsoft/TypeScript
keyof on generics arbitrarily returns `string` rather than the inferred values depending on the generic
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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).
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Ouvrez le TypeScript Playground lié et reproduisez l’erreur avec genericFunction, BaseSpec et les différents objets args. Cherchez pourquoi keyof Spec est élargi à string dans ce cas d’inférence générique, puis vérifiez que l’exemple passe la vérification de types et que le résultat inféré est "someKey".
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100