microsoft / microsoft/TypeScript
Infering type of optional parameter of lambda function is broken when passed to generic function.
Nessuno ha ancora preso questa issue.
- 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:
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
- 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
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