microsoft / microsoft/TypeScript
Cannot infer generic argument type from passed callback
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
infer, parameter, argument, callback, function
Code
function inferArguments<T>(callback: ((t: T) => void)) {
return callback;
}
function noop(){}
const explicit = inferArguments(({a = noop}: {a: Function}) => {});
explicit({a: noop}); // OK!
explicit({a: false}); // Expected error - Got one!
const implicit = inferArguments(({a = noop}) => {});
implicit({a: noop}); // OK!
implicit({a: false}); // Expected error - No Error!
Expected behavior:
Both function calls with ({a: false}) should cause a type error
Actual behavior:
Only the function call with the explicit typing causes a type error
Related Issues: #30975 Looks similar but seems different since there should be a clear way for inference to work in this case
Note that Parameters is correctly able to extract the correct types for such a construct as seen in this bit of code:
function noop() { }
function callback({ a = noop }) { }
let args: Parameters<typeof callback>;
args[0].a as Function
This appears to be an error with inline callback functions used in this fashion
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci i casi espliciti e impliciti nel TypeScript Playground collegato, quindi confronta i tipi dei parametri della callback inferiti e il comportamento di Parameters. L'issue non indica alcun file del repository né alcun test; il lavoro è completato quando entrambe le chiamate che usano {a: false} producono errori di tipo, mentre i casi noop validi continuano a essere accettati.
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à
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 48/100