microsoft / microsoft/TypeScript
Inside a generic function with a remapped type as parameter, elements are wrongfully inferred as {}.
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
### 🔎 Search Terms
remapped
key mapping
generics
Maybe related:
- https://github.com/microsoft/TypeScript/issues/47794
- https://github.com/microsoft/TypeScript/issues/48855
### 🕗 Version & Regression Information
- tested on playground v6.0.2
### ⏯ Playground Link
[Playground Link](https://www.typescriptlang.org/play/?declaration=false#code/PTAEBUFMGcBdoFAJKAQgVQOIEYkG9QUBtAaVAEsA7UOAJyoHMAyAa0gE8B7AM1AAlQAQ2igABp0oASPAGFBAB3KxBAG3IAvSAB4SAPgC+ogLoACgLy6I7eZFAByPPruh5tTgDdyAExihKnUABbQVgAYwALUG5OWlBYcNtocgZKEIBXWlsAOgRQPLjrWwAlSFTAyBIOaC0BSAAPWFKvERLQmK8tOkYAGiFKdl1LM1A8XPzx0gpqLspmNi5eAWExCWk5RWU1TR0DYwAuUAAKAEpQC1B3Tm8xvP0AbiRx7jTKUNhyCSjOTi1wUHrGpRmqBWu1OrB6LNeoJ+oNDuEYV4VJBaNADiUyhUqr9dKdRjdxl9aIc2pQ4H5BOUpqAEUDkai8QTCflkbAaYj6QcTmdLJdvGd2XSUdAiJijABCO6EMD+FiPZmEzKwDLUWlI4WiymQCUPBWgfTy5nxNwAdz8kDNAFFaG5iQAiTgsO3HXX5A0E0nkuoC6KcQ54CQAMW+XNO50c+hd0qOYd5Vy8DwNBGIZCoNAhjFYHB4-FM50dOXGsEKINKWsq7GqtQaTRakDatA6MwY0NhQxGTPykzTzazC1zoZ5F3jBPuhqiLzeH2ovt+-xrQLrDabGahfQGunhHOF6LL5Qr1XAuI7nby0WJnrZmOpavp0EZevGrMF6tog-OfK8AtvGrFkujjrjnqSoqi+d6auUOqnvqQH5ManBmpQFqgNatqHA6TouqO46XqA3rDL6-pBiGMZDhGUYoNyH7xomSAoBgmAAEz4NG3bTKuczZoseaWIcWT8YItAMGioAvCw-gmpQsbDtcRYlhi5bYtWgLAqCjbgpCLbrkQRiDAKox6mx6aaX2OZ8EYXL8VkgnCQcfCkEY0mfqODwEs8rzvJ8s5-ACtalsuGk9OucI-qiu6YgeOKMtB54khI5LXmmoX3h2j75CgJppLAAD8uXQU+kBsqF75xvywzJRB2qSrBzIgbQqrbqilVQXq7p6vBiHIahMToY6zqurcOHxWy+FfH6AaUMGnBcpABwACxzdJ5FSigsoIEmrGptQ8xmTxoAFgSxY2KWEVKfOKlLmCzatuwOl6cMBkKkZO3cZZAlCSJ9kkI5Q7OeMY5uZOnkzt8c6+Yu-lXRxN0hY1IkKfu2JHtFeqxbhiUNUKDKpWloDPsVpHUWVYG-lqOoAXK+X5HVmOviKf4DYSbUKh15pWjaPUYf12EesNeE+t8RGTSRhyzaAADMjFLfokYrcAgH6EAA)
### 💻 Code
```ts
type RenameKeys> = {
[K in string&keyof H as `on${Capitalize}`]: () => void
};
function foo>(handlers: RenameKeys) {
for(const name in handlers) {
let handler: () => void = handlers[name]!; // nok
// inferred as {} instead of () => void;
// Type 'NonNullable[Extract, string>]>' is not assignable to type '() => void'.
// Type '{}' provides no match for the signature '(): void'.(2322)
}
}
```
### 🙁 Actual behavior
The type of the elements of the remapped type is `{}` inside the generic function.
If we return the element, the returned type is correct (cf playground).
### 🙂 Expected behavior
The type should be either:
- `() = void` (correct)
- `unknown` or `any`: TS is unable to properly infer the type.
Having `{}` is quite strange/surprising.
### Additional information about the issue
There is another issue with `[K in string&keyof H]` which doesn't manifest when we use `[K in keyof H]` (cf playground):
```ts
type RenameKeys> = {
[K in string&keyof H]: (...args: H[K]) => void
};
function foo>(handlers: RenameKeys) {
for(const name in handlers)
return handlers[name]!;
throw new Error("ok");
}
const x = foo({onFoo: (e: 42) => {}}); // nok : (...args: unknown) => void
```
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
Commencez par la reproduction liée dans Playground et comparez le type remappé utilisant `string & keyof H` avec la variante `keyof H`. Suivez la manière dont l’accès à un élément est inféré dans la fonction générique `foo`. Le travail est terminé lorsque l’élément du handler remappé est inféré comme `() => void`, ou comme `unknown` ou `any` plutôt que `{}`, avec une couverture des cas signalés.
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é
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100