microsoft / microsoft/TypeScript

Inside a generic function with a remapped type as parameter, elements are wrongfully inferred as {}.

Aperta
#63,363 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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
```

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

Inizia con la riproduzione collegata in Playground e confronta il tipo rimappato che usa `string & keyof H` con la variante `keyof H`. Traccia come viene inferito l’accesso agli elementi all’interno della funzione generica `foo`. Il lavoro è completo quando l’elemento dell’handler rimappato viene inferito come `() => void`, oppure come `unknown` o `any` invece di `{}`, con una copertura dei casi segnalati.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.