microsoft / microsoft/TypeScript
Consider re-ordering Array#reduce overloads in lib.d.ts
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Example code:
type UnaryFunction = (arg1) => any;
type BinaryFunction = (arg1, arg2) => any;
let binaryFuncs: BinaryFunction[] = [];
let unaryFunc = arg1 => {};
let reduced = binaryFuncs.reduce((prev, next) => prev, unaryFunc);
// ACTUAL:
let f: UnaryFunction = reduced; // ERROR binary not assignable to unary
// EXPECTED:
let f: UnaryFunction = reduced; // OK - both lhs and rhs really are unary
The call to Array#reduce in the above example definitely returns a unary function, but the type system erroneously infers the return type as a binary function.
This seems to be caused by the declaration order of the two overloads of Array#reduce in lib.d.ts. If the declaration order is reversed, the problem is solved.
The two overloaded declarations in lib.d.ts are as follows:
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
The first overload matches the example with T=BinaryFunction, since it satisfies the compiler's assignability checks. So the second overload is never considered, even though it is a strictly better match, with T=BinaryFunction and U=UnaryFunction.
Would it be possible to swap the overload order for Array#reduce (and Array#reduceRight) in lib.d.ts to resolve this issue?
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 in lib.d.ts individuando i due overload di Array#reduce e i corrispondenti overload di Array#reduceRight. Riproduci l'esempio dell'issue, quindi verifica che invertire l'ordine degli overload renda il risultato inferito assegnabile a UnaryFunction per entrambi i metodi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 48/100