effect icon indicating copy to clipboard operation
effect copied to clipboard

Incorrect type for type/encodeSchema with Schema.declare

Open HugeLetters opened this issue 7 months ago • 2 comments

What version of Effect is running?

3.14.2

What steps can reproduce the bug?

basic issue is that a schema like this gives incorrect types for Schema.encodedSchema and Schema.typeSchema - because fundamentally both are gonna be the same as the original schema.

Schema.declare(
	[],
	{
		decode() {
			return function () {
				return ParseResult.succeed(1);
			};
		},
		encode() {
			return function () {
				return ParseResult.succeed("a");
			};
		},
	},
	{},
)

Here's a playground with a demo of the issue and my original use-case which lead me to discovering the issue https://effect.website/play#bc96e4ce0aef

Here's a discussion on your Discord server where I've initially reported the issue I was having, my use-case and how I've reached the core of the problem https://discord.com/channels/795981131316985866/1372606482985517056 My use-case was basically having a schema which can decode a HashMap from a string and back - decoding I do by myself, encoding I just delegate to Schema

What is the expected behavior?

I understand that there may not even be a solution here really

  1. There's no fixing type/encodedSchema so they give a schema with "expected" behavior - given how declare schemas are implemented I think it's straight up impossible. Perhaps Schema@4 could address this
  2. Perhaps the types could be corrected here so they at least represented what you actually get but that doesn't help much because
  3. because the way I've initially encountered the issue - I don't think point 2 would have helped much. So perhaps just some sort of disclaimer in docs?

What do you see instead?

I have solved my issue just by using HashMapFromSelf instead because then I get "consistent" encoding. I would understand if you decide to just close this issue as not planned because there's no obvious solution here - but I thought it's still worth to bring your attention to.

Additional information

No response

HugeLetters avatar May 16 '25 17:05 HugeLetters

My use-case was basically having a schema which can decode a HashMap from a string and back

Schema.declare is meant for defining new data types. In your case, you should use a transformation instead.

gcanti avatar May 17 '25 07:05 gcanti

Should Schema.declare perhaps enforce then that it should return the same type from decoding and encoding?

I didn't want to use a transform because then I'm validating data twice - during transform(decoding from string) and then on the type-side schema, that seemed excessive to me. Any workarounds for this? I could only think of using declare

HugeLetters avatar May 17 '25 10:05 HugeLetters