microsoft / microsoft/TypeScript
Inside a generic function with a remapped type as parameter, elements are wrongfully inferred as {}.
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 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
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Playground reproduction and compare the remapped type using `string & keyof H` with the `keyof H` variant. Trace how element access is inferred inside the generic `foo` function. Done means the remapped handler element is inferred as `() => void`, or as `unknown` or `any` rather than `{}`, with coverage for the reported cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100