microsoft / microsoft/TypeScript

Infering type of optional parameter of lambda function is broken when passed to generic function.

Offen
#33,852 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Type Inference
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:

https://www.typescriptlang.org/play/?ts=Nightly&ssl=11&ssc=23&pln=1&pc=1#code/CYUwxgNghgTiAEAzArgOzAFwJYHtX1RxwAcAeAFQD4AKANyggC55yBKZ8gbgFgAoPlOmx4k1KMwDOGGFlQBzADTwARvAC88AEwBmVvADe8OBmQx8UTvAC+fPmDxSkRdQSLFqYydNmKVLnXpqlPBQrJZ2DhgqsC6EJNSIYXzwfKCQsAiCmLj4ccSaFPAgAB4YIKjAEvDUAHR1xLBQALYSzFCoAJ4A2gC6gcHtHTT0TCzsLDz8vPaojohEmrFumh7i8FIy8kqqGgHqA2Ep05HRMIsaeSuJnEA

Notes
The mentioned versions of typescript are not exact, I just used TypeScript playground and the set of available versions there.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Ö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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.