-
-
Notifications
You must be signed in to change notification settings - Fork 33
Closed as not planned
Labels
Status: Awaiting FeedbackIssue or PR awaits feedback from the community.Issue or PR awaits feedback from the community.Type: IdeaMarks an idea, which might be accepted and implemented.Marks an idea, which might be accepted and implemented.
Description
Suggestion
A new rule that forbids the partial versions of reduce
and reduceRight
.
Given:
const arr: readonly string[] = ...; // may or may not be empty
Would be invalid according to suggested rule:
// compiles, throws TypeError: Reduce of empty array with no initial value
const foo = arr.reduce((a, b) => a);
// compiles, throws TypeError: Reduce of empty array with no initial value
const bar = arr.reduceRight((a, b) => a);
Would be valid according to suggested rule:
// compiles, doesn't throw
const foo = arr.reduce((a, b) => a + b, "");
// compiles, doesn't throw
const bar = arr.reduceRight((a, b) => a + b, "");
// if you can prove to tsc that you have at least one element, the form without `initialValue` is safe:
const arr: readonly [string, ...(readonly string[])] = [""];
const foo = arr.reduce((a, b) => a);
This is equivalent to the situation with reduce versus fold in Scala: https://www.wartremover.org/doc/warts.html#iterableops
Metadata
Metadata
Assignees
Labels
Status: Awaiting FeedbackIssue or PR awaits feedback from the community.Issue or PR awaits feedback from the community.Type: IdeaMarks an idea, which might be accepted and implemented.Marks an idea, which might be accepted and implemented.