Skip to content

Commit

Permalink
fix the type signature of use in Effect.Service (#3862)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
furrycatherder and tim-smart authored Nov 1, 2024
1 parent a503f8c commit 7386b71
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-buckets-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix the type signature of `use` in Effect.Service
9 changes: 9 additions & 0 deletions packages/effect/dtslint/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,3 +1341,12 @@ hole<
}>
>
>()

// $ExpectType { a: () => Effect<1, UnknownException, "R">; }
hole<
Simplify<
Effect.Tag.Proxy<"R", {
a: () => Promise<1>
}>
>
>()
10 changes: 8 additions & 2 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6502,6 +6502,8 @@ export declare namespace Tag {
: k
]: Type[k] extends (...args: infer Args extends ReadonlyArray<any>) => Effect<infer A, infer E, infer R> ?
(...args: Readonly<Args>) => Effect<A, E, Self | R>
: Type[k] extends (...args: infer Args extends ReadonlyArray<any>) => Promise<infer A> ?
(...args: Readonly<Args>) => Effect<A, Cause.UnknownException, Self>
: Type[k] extends (...args: infer Args extends ReadonlyArray<any>) => infer A ?
(...args: Readonly<Args>) => Effect<A, never, Self>
: Type[k] extends Effect<infer A, infer E, infer R> ? Effect<A, E, Self | R>
Expand Down Expand Up @@ -6560,7 +6562,9 @@ export const Tag: <const Id extends string>(id: Id) => <
& {
use: <X>(
body: (_: Type) => X
) => X extends Effect<infer A, infer E, infer R> ? Effect<A, E, R | Self> : Effect<X, never, Self>
) => [X] extends [Effect<infer A, infer E, infer R>] ? Effect<A, E, R | Self>
: [X] extends [PromiseLike<infer A>] ? Effect<A, Cause.UnknownException, Self>
: Effect<X, never, Self>
} = (id) => () => {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 2
Expand Down Expand Up @@ -6834,7 +6838,9 @@ export declare namespace Service {
}
readonly use: <X>(
body: (_: Self) => X
) => X extends Effect<infer A, infer E, infer R> ? Effect<A, E, R | Self> : Effect<X, never, Self>
) => [X] extends [Effect<infer A, infer E, infer R>] ? Effect<A, E, R | Self>
: [X] extends [PromiseLike<infer A>] ? Effect<A, Cause.UnknownException, Self>
: Effect<X, never, Self>
readonly make: (_: MakeService<Make>) => Self
}
& Context.Tag<Self, Self>
Expand Down
21 changes: 21 additions & 0 deletions packages/effect/test/Effect/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Cause } from "effect"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { pipe } from "effect/Function"
Expand Down Expand Up @@ -168,4 +169,24 @@ describe("Effect.Service", () => {
Effect.gen(function*() {
yield* Scoped.info("Ok").pipe(Effect.provide(Scoped.Default))
}))

it.effect("promises", () =>
Effect.gen(function*() {
class Service extends Effect.Service<Service>()("Service", {
succeed: {
foo: () => Promise.reject(new Error("foo")),
bar: () => Promise.resolve("bar")
},
accessors: true
}) {}

const withUse = yield* Service.foo().pipe(Effect.flip, Effect.provide(Service.Default))
expect(withUse).toEqual(new Cause.UnknownException(new Error("foo")))

const accessor = yield* Service.foo().pipe(Effect.flip, Effect.provide(Service.Default))
expect(accessor).toEqual(new Cause.UnknownException(new Error("foo")))

const accessorSuccess = yield* Service.bar().pipe(Effect.provide(Service.Default))
expect(accessorSuccess).toEqual("bar")
}))
})

0 comments on commit 7386b71

Please sign in to comment.