Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
effect-bot opened this issue Jan 8, 2024 · 3 comments
Closed

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

effect-bot opened this issue Jan 8, 2024 · 3 comments
Labels
bug Something isn't working schema

Comments

@effect-bot
Copy link

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

@mikearnaldi mikearnaldi added bug Something isn't working schema labels Jan 8, 2024
@gcanti
Copy link
Contributor

gcanti commented Jan 11, 2024

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?

@mikearnaldi
Copy link
Member

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"

@tim-smart
Copy link
Member

tim-smart commented Mar 18, 2024

Here is a POC: #2353

@gcanti gcanti closed this as completed Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working schema
Projects
Archived in project
Development

No branches or pull requests

4 participants