Skip to content

Expose the encoded type from TestSchema.Encoding.encodeUnknownEffect - #6937

Open
fubhy wants to merge 1 commit into
mainfrom
audit/repro-testing-test-schema-encoded-type
Open

Expose the encoded type from TestSchema.Encoding.encodeUnknownEffect#6937
fubhy wants to merge 1 commit into
mainfrom
audit/repro-testing-test-schema-encoded-type

Conversation

@fubhy

@fubhy fubhy commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

TestSchema.Encoding.encodeUnknownEffect is typed as succeeding with the decoded Type even though it returns the Encoded value.

Important

This PR starts with focused failing reproduction tests. Add the implementation fix to this same branch; CI is expected to fail until that fix is included.

Encoding.encodeUnknownEffect exposes the decoded type

Module: TestSchema
Audit ID: core-s-z-testing-test-schema-encoded-type
Severity / confidence: medium / high

What happens

TestSchema.Encoding.encodeUnknownEffect is typed as succeeding with the decoded Type even though it returns the Encoded value.

Why it happens

The public property is annotated as Effect.Effect<S["Type"], ...> even though SchemaParser.encodeUnknownEffect returns Effect.Effect<S["Encoded"], ...>, breaking type-safe composition for transformed schemas.

Expected behavior

An encoding operation succeeds with the schema's Encoded type.

Relevant implementation

These links and excerpts are pinned to audit base c9b56ab507f224426ee8388dc450da447ec4715f.

View problematic code at packages/effect/src/testing/TestSchema.ts:457-470
export class Encoding<S extends Schema.Constraint> {
  readonly schema: S
  readonly encodeUnknownEffect: (
    input: unknown,
    options?: SchemaAST.ParseOptions
  ) => Effect.Effect<S["Type"], SchemaIssue.Issue, S["EncodingServices"]>
  readonly options?: {
    readonly parseOptions?: SchemaAST.ParseOptions | undefined
  } | undefined
  constructor(schema: S, options?: {
    readonly parseOptions?: SchemaAST.ParseOptions | undefined
  }) {
    this.schema = schema
    this.encodeUnknownEffect = SchemaParser.encodeUnknownEffect(schema)

View exact lines on GitHub

View problematic code at packages/effect/src/SchemaParser.ts:600-610
export function encodeUnknownEffect<S extends Schema.Constraint>(
  schema: S,
  options?: SchemaAST.ParseOptions
): (
  input: unknown,
  options?: SchemaAST.ParseOptions
) => Effect.Effect<S["Encoded"], SchemaIssue.Issue, S["EncodingServices"]> {
  const parser = run<S["Encoded"], S["EncodingServices"]>(SchemaAST.flip(schema.ast))
  return options === undefined
    ? parser
    : (input, overrideOptions) => parser(input, mergeParseOptions(options, overrideOptions))

View exact lines on GitHub

Reproduction

pnpm test-types packages/effect/typetest/TestSchema.tst.ts

Observed failure: TypeScript 5.9.3 and 6.0.3 report Effect<number, Issue> is not Effect<string, Issue>

Implementation handoff

The initial reproduction tests on this branch are the regression specification for the implementation fix that should follow in this PR.

  1. Start with the pinned implementation excerpts and the Why it happens analysis above.
  2. Change the implementation so it satisfies the stated Expected behavior; do not weaken or remove the reproduction assertions.
  3. Run the focused reproduction command(s) and confirm the observed failures become passing tests:
pnpm test-types packages/effect/typetest/TestSchema.tst.ts
  1. Run the affected package's existing tests, then the repository lint and type checks before requesting review.

Audit provenance

  • Audit base: c9b56ab507f224426ee8388dc450da447ec4715f
  • Reproduction base: c9b56ab507f224426ee8388dc450da447ec4715f
  • Findings: core-s-z-testing-test-schema-encoded-type
  • Initial patch: focused reproduction tests; implementation fix pending

@fubhy fubhy added the audit Findings originating from the Effect runtime correctness audit label Aug 4, 2026
@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Aug 4, 2026
@changeset-bot

changeset-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f16a03f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@effect-slopcop effect-slopcop Bot added 4.0 bug Something isn't working labels Aug 4, 2026
@fubhy
fubhy requested a review from gcanti August 4, 2026 08:40

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important

This PR currently adds only the failing reproduction test. The one-line implementation fix is still missing from packages/effect/src/testing/TestSchema.ts.

Reviewed changes

  • Adds a TSTyche type test in packages/effect/typetest/TestSchema.tst.ts that asserts Encoding.encodeUnknownEffect for Schema.NumberFromString produces Effect<string, SchemaIssue.Issue>.

I ran the targeted type test and confirmed it fails as expected before the fix: the current type is Effect<number, Issue, never> because TestSchema.Encoding.encodeUnknownEffect is declared with S["Type"] instead of S["Encoded"].

⚠️ Implementation fix not yet included

The root cause is outside this diff, so it cannot be resolved by editing the test file alone. In packages/effect/src/testing/TestSchema.ts, the encodeUnknownEffect field is annotated as Effect.Effect<S["Type"], ...>, while the SchemaParser.encodeUnknownEffect call assigned to it returns Effect.Effect<S["Encoded"], ...>. Update the annotation to S["Encoded"] so the public type matches the runtime value.

Technical details
# Fix the `Encoding.encodeUnknownEffect` return type

## Affected sites
- `packages/effect/src/testing/TestSchema.ts:462``encodeUnknownEffect` uses `S["Type"]` instead of `S["Encoded"]`
- `packages/effect/typetest/TestSchema.tst.ts:10` — regression test that verifies `Effect<string, SchemaIssue.Issue>` for `NumberFromString`

## Required outcome
- `TestSchema.Encoding.encodeUnknownEffect` is typed as `Effect.Effect<S["Encoded"], SchemaIssue.Issue, S["EncodingServices"]>`.
- `pnpm test-types packages/effect/typetest/TestSchema.tst.ts` passes.

## Suggested approach
In `packages/effect/src/testing/TestSchema.ts`, change line 462 from:

```ts
) => Effect.Effect<S["Type"], SchemaIssue.Issue, S["EncodingServices"]>
```

to:

```ts
) => Effect.Effect<S["Encoded"], SchemaIssue.Issue, S["EncodingServices"]>
```

## Follow-ups
- Add a `.changeset` entry because this corrects an exported public API type.
- Run `pnpm lint`, `pnpm check`, and `pnpm test-types packages/effect/typetest/TestSchema.tst.ts` after applying the fix.

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | Fix all ➔Fix 👍s ➔View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

it("types Encoding.encodeUnknownEffect with the encoded output", () => {
const encoding = new TestSchema.Asserts(Schema.NumberFromString).encoding()

expect(encoding.encodeUnknownEffect(1)).type.toBe<Effect.Effect<string, SchemaIssue.Issue>>()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This assertion is the correct regression spec. It currently fails because TestSchema.Encoding.encodeUnknownEffect is still declared with S["Type"] in packages/effect/src/testing/TestSchema.ts:462; once that annotation is corrected to S["Encoded"], this test should pass.

@github-project-automation github-project-automation Bot moved this from Discussion Ongoing to Waiting on Author in PR Backlog Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 audit Findings originating from the Effect runtime correctness audit bug Something isn't working

Projects

Status: Waiting on Author

Development

Successfully merging this pull request may close these issues.

1 participant