microsoft / microsoft/TypeScript
Infering type of optional parameter of lambda function is broken when passed to generic function.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
TypeScript Version: 3.7.0-beta, nightly and older ones
Search Terms:
optional parameter infer lambda generic
Code
declare function noop<T>(val: T): T;
const f = (a: string, b = 23) => b;
// reports "Parameter 'b' implicitly has an 'any' type."
// broken since v3.5.1, older versions work well
const foo = noop((a: string, b = 23) => b);
// (a: string, b?: number) => number
const bar = noop(f);
declare function noop2<T extends (...params: any[]) => any>(val: T): T;
// (a: string, b?: any) => any (BUG)
const foo2 = noop2((a = 23) => a); // broken on all versions
// (a: string, b?: number) => number
const bar2 = noop2(f);
Expected behavior:
foo* and bar* should all have the same type.
Actual behavior:
In the first case, the compiler complains that it can't infer the type of optional argument of lambda forwarded to noop, and in the second case it silently assigns any type to b instead of number
Playground Link:
Notes
The mentioned versions of typescript are not exact, I just used TypeScript playground and the set of available versions there.
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
Beginne damit, die Beispiele im verknüpften TypeScript Playground zu reproduzieren, insbesondere die Aufrufe von noop und noop2 mit optionalen Lambda-Parametern. Verfolge die generische Inferenz und das kontextuelle Typisieren des Compilers für diese Ausdrücke. Als abgeschlossen gilt die Aufgabe, wenn foo, bar, foo2 und bar2 äquivalente Parameter- und Rückgabetypen ableiten, ohne einen implicit any-Fehler.
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
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100