Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-recurwhile-recuruntil-type-inference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix `Schedule.recurWhile` and `Schedule.recurUntil` not inferring the predicate argument type when used with `Effect.repeat`.
21 changes: 20 additions & 1 deletion packages/effect/dtslint/Schedule.tst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Console, Schedule } from "effect"
import { Console, Effect, Schedule } from "effect"
import { describe, expect, it, when } from "tstyche"

describe("Schedule", () => {
Expand Down Expand Up @@ -31,4 +31,23 @@ describe("Schedule", () => {
)
)
})

it("recurWhile / recurUntil predicate argument inference", () => {
// predicate arg should infer from the In type of the surrounding Effect.repeat, not default to unknown
const stringEffect = Effect.sync(() => "foo")

expect(stringEffect.pipe(
Effect.repeat(Schedule.recurWhile((data) => {
expect(data).type.toBe<string>()
return data === "foo"
}))
)).type.toBe<Effect.Effect<string, never, never>>()

expect(stringEffect.pipe(
Effect.repeat(Schedule.recurUntil((data) => {
expect(data).type.toBe<string>()
return data === "foo"
}))
)).type.toBe<Effect.Effect<string, never, never>>()
})
})
8 changes: 4 additions & 4 deletions packages/effect/src/Schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export const collectAllOutputs: <Out, In, R>(self: Schedule<Out, In, R>) => Sche
* @since 2.0.0
* @category Collecting
*/
export const collectUntil: <A>(f: Predicate<A>) => Schedule<Chunk.Chunk<A>, A> = internal.collectUntil
export const collectUntil: <A>(f: Predicate<Types.NoInfer<A>>) => Schedule<Chunk.Chunk<A>, A> = internal.collectUntil

/**
* Collects all inputs into a `Chunk` until an effectful condition fails.
Expand Down Expand Up @@ -487,7 +487,7 @@ export const collectUntilEffect: <A, R>(
* @since 2.0.0
* @category Collecting
*/
export const collectWhile: <A>(f: Predicate<A>) => Schedule<Chunk.Chunk<A>, A> = internal.collectWhile
export const collectWhile: <A>(f: Predicate<Types.NoInfer<A>>) => Schedule<Chunk.Chunk<A>, A> = internal.collectWhile

/**
* Collects all inputs into a `Chunk` while an effectful condition holds.
Expand Down Expand Up @@ -1495,7 +1495,7 @@ export const provideService: {
* @since 2.0.0
* @category Recurrence Conditions
*/
export const recurUntil: <A>(f: Predicate<A>) => Schedule<A, A> = internal.recurUntil
export const recurUntil: <A>(f: Predicate<Types.NoInfer<A>>) => Schedule<A, A> = internal.recurUntil

/**
* A schedule that recurs until the given effectful predicate evaluates to true.
Expand Down Expand Up @@ -1568,7 +1568,7 @@ export const recurUpTo: (duration: Duration.DurationInput) => Schedule<Duration.
* @since 2.0.0
* @category Recurrence Conditions
*/
export const recurWhile: <A>(f: Predicate<A>) => Schedule<A, A> = internal.recurWhile
export const recurWhile: <A>(f: Predicate<Types.NoInfer<A>>) => Schedule<A, A> = internal.recurWhile

/**
* A schedule that recurs as long as the given effectful predicate evaluates to
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/test/Schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("Schedule", () => {
}))
it.effect("union of two schedules should continue as long as either wants to continue", () =>
Effect.gen(function*() {
const schedule = Schedule.recurWhile((b: boolean) => b).pipe(Schedule.union(Schedule.fixed("1 seconds")))
const schedule = Schedule.recurWhile<boolean>((b) => b).pipe(Schedule.union(Schedule.fixed("1 seconds")))
const input = Chunk.make(true, false, false, false, false)
const result = yield* runCollect(schedule.pipe(Schedule.compose(Schedule.elapsed)), input)
const expected = [0, 0, 1, 2, 3].map(Duration.seconds)
Expand Down
Loading