effect
effect copied to clipboard
From Discord: The Possibility of `schema.pipe(S.optional)`
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 asS.optional(schema)
.
Discord thread
https://discord.com/channels/795981131316985866/1193893038557249566
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?
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 isSchema<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"
Here is a POC: https://github.com/Effect-TS/effect/pull/2353