microsoft / microsoft/TypeScript
Partial type for reduce initialValue
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
reduce initialvalue partial type assertion
Suggestion
Add a signature to the type for Array.reduce<U> to allow the initialValue to be a Partial<U>
Use Cases
Many cases where the reduce function is used to build an object that will eventually conform to U, but starts with an empty accumulator.
Examples
Simple example where the initialValue does not satisfy U but the final result will:
['one'].reduce<{ one: string }>((acc, i) => ({ [i]: i, ...acc }), {})
This yields
Argument of type '{}' is not assignable to parameter of type '{ one: string; }'.
Property 'one' is missing in type '{}' but required in type '{ one: string; }'.
Calling the function without an initialValue causes problems because the initialValue needs to be an object for this to work.
The below type signature would fix this issue:
interface Array<T> {
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: Partial<U>): U;
}
Checklist
My suggestion meets these guidelines:
- [?] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 esaminando la firma di Array.reduce descritta nell’issue e l’esempio che usa un oggetto vuoto come initialValue. Verifica in che modo il parametro Partial proposto influisce sulla sicurezza dei tipi e sugli overload esistenti di reduce. Il lavoro sarà considerato completato quando la proposta sarà risolta con un design del sistema dei tipi concordato e la relativa validazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100