Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-drinks-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Refine existing type in Predicate.isTagged
4 changes: 2 additions & 2 deletions packages/effect/src/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,8 +1164,8 @@ export const hasProperty: {
* @since 2.0.0
*/
export const isTagged: {
<K extends string>(tag: K): (self: unknown) => self is { _tag: K }
<K extends string>(self: unknown, tag: K): self is { _tag: K }
<K extends string>(tag: K): <T>(self: T) => self is T extends {} ? Extract<T, { _tag: K }> : T & { _tag: K }
<K extends string, T>(self: T, tag: K): self is T extends {} ? Extract<T, { _tag: K }> : T & { _tag: K }
Comment on lines +1167 to +1168

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

pnpm test-types packages/effect/typetest/Predicate.tst.ts
rg -n 'ReadonlyArray<object>|Record<string, unknown>|isTagged' packages/effect/typetest packages/effect/src/Predicate.ts

Repository: Effect-TS/effect

Length of output: 535


Preserve narrowing for broad object inputs.

T extends {} routes object and Record<string, unknown> through Extract, which collapses to never for values that can still pass the runtime _tag check. Use T extends { _tag: K } ? T : T & { _tag: K } in both overloads so tagged unions stay precise and broad inputs still narrow correctly.

Suggested refinement
-  <K extends string>(tag: K): <T>(self: T) => self is T extends {} ? Extract<T, { _tag: K }> : T & { _tag: K }
-  <K extends string, T>(self: T, tag: K): self is T extends {} ? Extract<T, { _tag: K }> : T & { _tag: K }
+  <K extends string>(tag: K): <T>(self: T) => self is T extends { _tag: K } ? T : T & { _tag: K }
+  <K extends string, T>(self: T, tag: K): self is T extends { _tag: K } ? T : T & { _tag: K }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<K extends string>(tag: K): <T>(self: T) => self is T extends {} ? Extract<T, { _tag: K }> : T & { _tag: K }
<K extends string, T>(self: T, tag: K): self is T extends {} ? Extract<T, { _tag: K }> : T & { _tag: K }
<K extends string>(tag: K): <T>(self: T) => self is T extends { _tag: K } ? T : T & { _tag: K }
<K extends string, T>(self: T, tag: K): self is T extends { _tag: K } ? T : T & { _tag: K }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/effect/src/Predicate.ts` around lines 1167 - 1168, Update both
overloads of the tag predicate to replace the T extends {} conditional with T
extends { _tag: K } ? T : T & { _tag: K }. Preserve precise narrowing for
already-tagged unions while allowing broad object inputs to narrow to the
required _tag shape.

Source: MCP tools

} = dual(
2,
<K extends string>(self: unknown, tag: K): self is { _tag: K } => hasProperty(self, "_tag") && self["_tag"] === tag
Expand Down
8 changes: 7 additions & 1 deletion packages/effect/typetest/Predicate.tst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare const unknowns: ReadonlyArray<unknown>
declare const numberOrNull: ReadonlyArray<number | null>
declare const numberOrUndefined: ReadonlyArray<number | undefined>
declare const numberOrNullOrUndefined: ReadonlyArray<number | null | undefined>
declare const tags: ReadonlyArray<{ _tag: "a"; a: string } | { _tag: "b"; b: string }>
declare const tagsOrNumbers: ReadonlyArray<{ _tag: "a"; a: string } | { _tag: "b"; b: string } | number>

describe("Predicate", () => {
it("isString", () => {
Expand Down Expand Up @@ -56,7 +58,11 @@ describe("Predicate", () => {
})

it("isTagged", () => {
expect(anys.filter(Predicate.isTagged("a"))).type.toBe<Array<{ _tag: "a" }>>()
expect(anys.filter(Predicate.isTagged("a"))).type.toBe<Array<any>>()
expect(unknowns.filter(Predicate.isTagged("a"))).type.toBe<Array<{ _tag: "a" }>>()
expect(tags.filter(Predicate.isTagged("a"))).type.toBe<Array<{ _tag: "a"; a: string }>>()
expect(tagsOrNumbers.filter(Predicate.isTagged("a"))).type.toBe<Array<{ _tag: "a"; a: string }>>()
expect(numberOrNull.filter(Predicate.isTagged("a"))).type.toBe<Array<never>>()
})

it("isNullish", () => {
Expand Down