-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schedule: fix unsafe
tapOutput
signature
- Loading branch information
Showing
5 changed files
with
105 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
"effect": patch | ||
--- | ||
|
||
Schedule: fix unsafe `tapOutput` signature. | ||
|
||
Previously, `tapOutput` allowed using an output type that wasn't properly inferred, leading to potential runtime errors. Now, TypeScript correctly detects mismatches at compile time, preventing unexpected crashes. | ||
|
||
**Before (Unsafe, Causes Runtime Error)** | ||
|
||
```ts | ||
import { Effect, Schedule, Console } from "effect" | ||
|
||
const schedule = Schedule.once.pipe( | ||
Schedule.as<number | string>(1), | ||
Schedule.tapOutput((s: string) => Console.log(s.trim())) // ❌ Runtime error | ||
) | ||
|
||
Effect.runPromise(Effect.void.pipe(Effect.schedule(schedule))) | ||
// throws: TypeError: s.trim is not a function | ||
``` | ||
|
||
**After (Safe, Catches Type Error at Compile Time)** | ||
|
||
```ts | ||
import { Console, Schedule } from "effect" | ||
|
||
const schedule = Schedule.once.pipe( | ||
Schedule.as<number | string>(1), | ||
// ✅ Type Error: Type 'number' is not assignable to type 'string' | ||
Schedule.tapOutput((s: string) => Console.log(s.trim())) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Console, Schedule } from "effect" | ||
|
||
// ------------------------------------------------------------------------------------- | ||
// tapOutput | ||
// ------------------------------------------------------------------------------------- | ||
|
||
// $ExpectType Schedule<string | number, unknown, never> | ||
Schedule.once.pipe( | ||
Schedule.as<number | string>(1), | ||
Schedule.tapOutput(( | ||
x // $ExpectType string | number | ||
) => Console.log(x)) | ||
) | ||
|
||
// The callback should not affect the type of the output (`number`) | ||
// $ExpectType Schedule<number, unknown, never> | ||
Schedule.once.pipe( | ||
Schedule.as(1), | ||
Schedule.tapOutput((x: string | number) => Console.log(x)) | ||
) | ||
// $ExpectType Schedule<number, unknown, never> | ||
Schedule.tapOutput( | ||
Schedule.once.pipe( | ||
Schedule.as(1) | ||
), | ||
(x: string | number) => Console.log(x) | ||
) | ||
|
||
Schedule.once.pipe( | ||
Schedule.as<number | string>(1), | ||
// @ts-expect-error | ||
Schedule.tapOutput((s: string) => Console.log(s.trim())) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters