Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Better type for reduce if not ininitial value is given#2205

Merged
jdeniau merged 1 commit into
mainimmutable-js/immutable-js:mainfrom
reduce-typeimmutable-js/immutable-js:reduce-typeCopy head branch name to clipboard
Jun 1, 2026
Merged

Better type for reduce if not ininitial value is given#2205
jdeniau merged 1 commit into
mainimmutable-js/immutable-js:mainfrom
reduce-typeimmutable-js/immutable-js:reduce-typeCopy head branch name to clipboard

Conversation

@jdeniau

@jdeniau jdeniau commented Jun 1, 2026

Copy link
Copy Markdown
Member

[TypeScript]: reduce/reduceRight without an initial value now infer the result type from the collection's values when the reducer returns a value (e.g. list.reduce((a, b) => a + b) infers number), matching Array#reduce. Previously an explicit type argument was required.


test('reduce', () => {
// With an initial value, the result type is the accumulator type `R`.
(l: List<number>) => {
};

// With an initial value (overload 1), the reduction is typed `R`.
(l: List<number>) => {

// Without an initial value and a reducer that returns a value (`R = V`),
// the result type `V` is inferred from the values — no annotation needed.
(l: List<number>) => {

// Without an initial value but a reducer that returns a different type `R`,
// the reduction is typed `V | R`, since the first value (a `V`) seeds it.
(l: List<number>) => {
};

// Keyed collection: key is `K`.
(m: Map<string, number>) => {
};

// Set collection: value and key are both `T`.
(s: Set<number>) => {
};

// Base Collection.
(c: Collection<string, number>) => {
});

test('reduceRight', () => {
(l: List<number>) => {
).type.toBe<string>();
};

(l: List<number>) => {
@jdeniau
jdeniau merged commit 383cae8 into main Jun 1, 2026
10 checks passed
@jdeniau
jdeniau deleted the reduce-type branch June 1, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.