microsoft / microsoft/TypeScript
Cannot infer generic argument type from passed callback
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere die expliziten und impliziten Fälle im verlinkten TypeScript Playground und vergleiche dann deren abgeleitete Callback-Parametertypen sowie das Verhalten von Parameters. Das Issue nennt keine Repository-Datei und keinen Test; erledigt ist es, wenn beide Aufrufe mit {a: false} Typfehler erzeugen, während die gültigen noop-Fälle weiterhin akzeptiert werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100