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.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
typescript
Ambito
compilers

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.

Descrizione

Needs Investigation
🔎 Search Terms

remapped
key mapping
generics

Maybe related:

🕗 Version & Regression Information
  • tested on playground v6.0.2
⏯ Playground Link

Playground Link

💻 Code
    type RenameKeys<H extends Record<string, any>> = {
        [K in string&keyof H as `on${Capitalize<K>}`]: () => void
    };

    function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {

        for(const name in handlers) {
            let handler: () => void = handlers[name]!; // nok
            // inferred as {} instead of () => void;
            // Type 'NonNullable<RenameKeys<T>[Extract<keyof RenameKeys<T>, 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):

type RenameKeys<H extends Record<string, any[]>> = {
     [K in string&keyof H]: (...args: H[K]) => void
};

function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {

        for(const name in handlers)
            return handlers[name]!;

        throw new Error("ok");
}

const x = foo({onFoo: (e: 42) => {}}); // nok : (...args: unknown) => void
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

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.

Altre issue di microsoft/TypeScript

Tutte le issue di microsoft/TypeScript

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.