Skip to content

Commit a02470c

Browse files
fix Context.add Context.make fns (#5031)
Co-authored-by: Tim <hello@timsmart.co>
1 parent a5f7595 commit a02470c

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

.changeset/flat-buses-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix Context.add & Context.make signatures

packages/effect/dtslint/Context.tst.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@ describe("Context", () => {
1212
class C extends Context.Reference<C>()("C", { defaultValue: () => 0 }) {}
1313
expect(C.key).type.toBe<"C">()
1414
})
15+
it("Tag with static fields", () => {
16+
class Foo extends Context.Tag("Foo")<Foo, { readonly bar: string }>() {
17+
static readonly StaticField = "StaticField"
18+
}
19+
20+
// @ts-expect-error
21+
Context.empty().pipe(Context.add(Foo, 123))
22+
23+
const ctx = Context.empty().pipe(Context.add(Foo, { bar: "2" }))
24+
expect(ctx).type.toBe<Context.Context<Foo>>()
25+
})
1526
})

packages/effect/src/Context.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ export const empty: () => Context<never> = internal.empty
262262
* @since 2.0.0
263263
* @category constructors
264264
*/
265-
export const make: <T extends Tag<any, any>>(tag: T, service: Tag.Service<T>) => Context<Tag.Identifier<T>> =
266-
internal.make
265+
export const make: <I, S>(tag: Tag<I, S>, service: Types.NoInfer<S>) => Context<I> = internal.make
267266

268267
/**
269268
* Adds a service to a given `Context`.
@@ -290,15 +289,8 @@ export const make: <T extends Tag<any, any>>(tag: T, service: Tag.Service<T>) =>
290289
* @since 2.0.0
291290
*/
292291
export const add: {
293-
<T extends Tag<any, any>>(
294-
tag: T,
295-
service: Tag.Service<T>
296-
): <Services>(self: Context<Services>) => Context<Services | Tag.Identifier<T>>
297-
<Services, T extends Tag<any, any>>(
298-
self: Context<Services>,
299-
tag: T,
300-
service: Tag.Service<T>
301-
): Context<Services | Tag.Identifier<T>>
292+
<I, S>(tag: Tag<I, S>, service: Types.NoInfer<S>): <Services>(self: Context<Services>) => Context<Services | I>
293+
<Services, I, S>(self: Context<Services>, tag: Tag<I, S>, service: Types.NoInfer<S>): Context<Services | I>
302294
} = internal.add
303295

304296
/**

packages/effect/src/internal/context.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type * as O from "../Option.js"
99
import { pipeArguments } from "../Pipeable.js"
1010
import { hasProperty } from "../Predicate.js"
1111
import type * as STM from "../STM.js"
12+
import type { NoInfer } from "../Types.js"
1213
import { EffectPrototype, effectVariance } from "./effectable.js"
1314
import * as option from "./option.js"
1415

@@ -202,24 +203,22 @@ const _empty = makeContext(new Map())
202203
export const empty = (): C.Context<never> => _empty
203204

204205
/** @internal */
205-
export const make = <T extends C.Tag<any, any>>(
206-
tag: T,
207-
service: C.Tag.Service<T>
208-
): C.Context<C.Tag.Identifier<T>> => makeContext(new Map([[tag.key, service]]))
206+
export const make = <I, S>(tag: C.Tag<I, S>, service: NoInfer<S>): C.Context<I> =>
207+
makeContext(new Map([[tag.key, service]]))
209208

210209
/** @internal */
211210
export const add = dual<
212-
<T extends C.Tag<any, any>>(
213-
tag: T,
214-
service: C.Tag.Service<T>
211+
<I, S>(
212+
tag: C.Tag<I, S>,
213+
service: NoInfer<S>
215214
) => <Services>(
216215
self: C.Context<Services>
217-
) => C.Context<Services | C.Tag.Identifier<T>>,
218-
<Services, T extends C.Tag<any, any>>(
216+
) => C.Context<Services | I>,
217+
<Services, I, S>(
219218
self: C.Context<Services>,
220-
tag: T,
221-
service: C.Tag.Service<T>
222-
) => C.Context<Services | C.Tag.Identifier<T>>
219+
tag: C.Tag<I, S>,
220+
service: NoInfer<S>
221+
) => C.Context<Services | I>
223222
>(3, (self, tag, service) => {
224223
const map = new Map(self.unsafeMap)
225224
map.set(tag.key, service)

packages/effect/test/Context.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ describe("Context", () => {
113113
readonly FooBar: unique symbol
114114
}
115115
const Service = Context.GenericTag<FooBar, Foo | Bar>("FooBar")
116-
const context = Context.make(Service, { _tag: "Foo" })
116+
const context = Context.make(Service, { _tag: "Foo" }).pipe(
117+
Context.add(Service, { _tag: "Foo" })
118+
)
117119
deepStrictEqual(Context.get(context, Service), { _tag: "Foo" })
118120
})
119121

0 commit comments

Comments
 (0)