diff --git a/.changeset/perfect-buckets-battle.md b/.changeset/perfect-buckets-battle.md new file mode 100644 index 00000000000..8c1fa41b794 --- /dev/null +++ b/.changeset/perfect-buckets-battle.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +fix the type signature of `use` in Effect.Service diff --git a/packages/effect/dtslint/Effect.ts b/packages/effect/dtslint/Effect.ts index 672f97434db..2f4b02e7063 100644 --- a/packages/effect/dtslint/Effect.ts +++ b/packages/effect/dtslint/Effect.ts @@ -1341,3 +1341,12 @@ hole< }> > >() + +// $ExpectType { a: () => Effect<1, UnknownException, "R">; } +hole< + Simplify< + Effect.Tag.Proxy<"R", { + a: () => Promise<1> + }> + > +>() diff --git a/packages/effect/src/Effect.ts b/packages/effect/src/Effect.ts index fdc4b05e179..f797841e54d 100644 --- a/packages/effect/src/Effect.ts +++ b/packages/effect/src/Effect.ts @@ -6502,6 +6502,8 @@ export declare namespace Tag { : k ]: Type[k] extends (...args: infer Args extends ReadonlyArray) => Effect ? (...args: Readonly) => Effect + : Type[k] extends (...args: infer Args extends ReadonlyArray) => Promise ? + (...args: Readonly) => Effect : Type[k] extends (...args: infer Args extends ReadonlyArray) => infer A ? (...args: Readonly) => Effect : Type[k] extends Effect ? Effect @@ -6560,7 +6562,9 @@ export const Tag: (id: Id) => < & { use: ( body: (_: Type) => X - ) => X extends Effect ? Effect : Effect + ) => [X] extends [Effect] ? Effect + : [X] extends [PromiseLike] ? Effect + : Effect } = (id) => () => { const limit = Error.stackTraceLimit Error.stackTraceLimit = 2 @@ -6834,7 +6838,9 @@ export declare namespace Service { } readonly use: ( body: (_: Self) => X - ) => X extends Effect ? Effect : Effect + ) => [X] extends [Effect] ? Effect + : [X] extends [PromiseLike] ? Effect + : Effect readonly make: (_: MakeService) => Self } & Context.Tag diff --git a/packages/effect/test/Effect/service.test.ts b/packages/effect/test/Effect/service.test.ts index f515c80925d..07a05ba01b3 100644 --- a/packages/effect/test/Effect/service.test.ts +++ b/packages/effect/test/Effect/service.test.ts @@ -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" @@ -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", { + 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") + })) })