Skip to content

Commit

Permalink
Added type-level validation for the Effect.Service (#4298)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <[email protected]>
  • Loading branch information
KhraksMamtsov and tim-smart authored Jan 21, 2025
1 parent b2a31be commit 8b4e75d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .changeset/late-bees-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"effect": patch
---

Added type-level validation for the `Effect.Service` function to ensure the `Self` generic parameter is provided. If the generic is missing, the `MissingSelfGeneric` type will be returned, indicating that the generic parameter must be specified. This improves type safety and prevents misuse of the `Effect.Service` function.

```ts
type MissingSelfGeneric =
`Missing \`Self\` generic - use \`class Self extends Service<Self>()...\``
```
7 changes: 5 additions & 2 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13013,6 +13013,9 @@ export const Tag: <const Id extends string>(id: Id) => <
return makeTagProxy(TagClass as any)
}

/** @internal */
type MissingSelfGeneric = `Missing \`Self\` generic - use \`class Self extends Effect.Service<Self>()...\``

/**
* Simplifies the creation and management of services in Effect by defining both
* a `Tag` and a `Layer`.
Expand Down Expand Up @@ -13057,7 +13060,7 @@ export const Tag: <const Id extends string>(id: Id) => <
* @category Context
* @experimental might be up for breaking changes
*/
export const Service: <Self>() => {
export const Service: <Self = never>() => [Self] extends [never] ? MissingSelfGeneric : {
<
const Key extends string,
const Make extends
Expand Down Expand Up @@ -13230,7 +13233,7 @@ export const Service: <Self>() => {

return proxy === true ? makeTagProxy(TagClass) : TagClass
}
}
} as any

/**
* @since 3.9.0
Expand Down

0 comments on commit 8b4e75d

Please sign in to comment.