microsoft / microsoft/TypeScript

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

Aperta
#33,852 0 commenti 1 reazione 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
1g 19h
PR unite (30g)
117

Descrizione

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.

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 riproducendo gli esempi nel TypeScript Playground collegato, in particolare le chiamate a noop e noop2 con parametri lambda opzionali. Traccia l'inferenza generica e la tipizzazione contestuale del compilatore per queste espressioni. Il lavoro è completato quando foo, bar, foo2 e bar2 inferiscono tipi dei parametri e di ritorno equivalenti senza un errore di implicit any.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.