microsoft / microsoft/TypeScript

Incomplete infer for generic types

Aperta
#41,456 3 commenti 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 4.0.5

Search Terms:
typescript, infer, incomplete, unknown, generic

Expected behavior:
The compiler should be able to extract information of the generic Item type from the function definition and infer string.

Actual behavior:
The compiler is able to infer the Names type as { id: string } but the Item type is unknown.

Related Issues:

Code

interface LoaderConfig<Key, Item> {
  loadItems: (keys: readonly Key[]) => Promise<readonly Item[]>;
  getKey: (item: Item) => Key;
  makeCacheKey: (key: Key) => string;
}

type LoaderConfigByName<Names, Item> = {
  [name in keyof Names]: LoaderConfig<Names[name], Item>;
};

function makeLoader<Names, Item>(
  configs: LoaderConfigByName<Names, Item>
): void {
  console.log(configs);
}

makeLoader({
  id: {
    loadItems: async (keys: readonly string[]): Promise<readonly string[]> => {
      return await Promise.resolve(keys);
    },
    getKey: (item: string): string => item,
    makeCacheKey: (key: string): string => {
      return key;
    }
  }
});
Output
"use strict";
function makeLoader(configs) {
    console.log(configs);
}
makeLoader({
    id: {
        loadItems: async (keys) => {
            return await Promise.resolve(keys);
        },
        getKey: (item) => item,
        makeCacheKey: (key) => {
            return key;
        }
    }
});

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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 il link Playground fornito e con l’esempio generico makeLoader usando TypeScript 4.0.5. Riproduci il risultato dell’inferenza e traccia la gestione da parte del compilatore del tipo generico Item. Il lavoro è completato quando l’esempio inferisce Item come string mantenendo il tipo Names inferito.

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à
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.