effect icon indicating copy to clipboard operation
effect copied to clipboard

From Discord: The Possibility of `schema.pipe(S.optional)`

Open effect-bot opened this issue 1 year ago • 2 comments

Summary

The user noticed that schema.pipe(S.optional) is not working anymore and received an error message stating that the 'this' context of type 'Schema' is not assignable. Another user confirmed that the syntax has changed and it should now be used as S.optional(schema). The user mentioned that they have already migrated their codebase and luckily didn't encounter too many occurrences of this issue.

Key takeaways:

  • The syntax for using S.optional has changed.
  • Instead of schema.pipe(S.optional), it should now be used as S.optional(schema).

Discord thread

https://discord.com/channels/795981131316985866/1193893038557249566

effect-bot avatar Jan 08 '24 13:01 effect-bot

It seems that ts encounters difficulty inferring the correct type when optional is used tacitly:

const x = S.string.pipe(S.optional); // error

const y = pipe(S.string, S.optional); // error

This might be due to the presence of different overloads in the optional signature. In both cases above, the inferred schema is Schema<unknown, unknown>.

I'm unsure about how to resolve this issue. However, isn't tacit usage never recommended?

gcanti avatar Jan 11 '24 16:01 gcanti

It seems that ts encounters difficulty inferring the correct type when optional is used tacitly:

const x = S.string.pipe(S.optional); // error

const y = pipe(S.string, S.optional); // error

This might be due to the presence of different overloads in the optional signature. In both cases above, the inferred schema is Schema<unknown, unknown>.

I'm unsure about how to resolve this issue. However, isn't tacit usage never recommended?

While tacit usage is never recommended pipe is kind of an exceptional case, otherwise we could also say that pipe([0, 1], ReadonlyArray.map((n) => n + 1) is discouraged.

Re overloads, this is indeed the issue, namely overload selection, I think we need to decide if the reason we have overloads is absolute necessity or if we can encode what we do with overloads with a conditional return type (that would solve tacit usage).

It's up to @gcanti if you decide that overloads are needed then feel free to close the issue as "intended behaviour"

mikearnaldi avatar Jan 31 '24 18:01 mikearnaldi

Here is a POC: https://github.com/Effect-TS/effect/pull/2353

tim-smart avatar Mar 18 '24 02:03 tim-smart