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: Issue with nesting Data.TaggedEnum resulting in never type #1497

Closed
effect-bot opened this issue Oct 7, 2023 · 3 comments · Fixed by #1521
Closed

From Discord: Issue with nesting Data.TaggedEnum resulting in never type #1497

effect-bot opened this issue Oct 7, 2023 · 3 comments · Fixed by #1521

Comments

@effect-bot
Copy link

Summary

In the above conversation, there was a discussion about an issue with nesting Data.TaggedEnum resulting in a never type. The initial suggestion was to change the definition of TaggedEnum to include a value field instead of intersecting it with { _tag: Tag }. This would allow for easier construction of nested sum types and improve the developer experience.

However, there were different opinions on whether this change is desirable. One argument was that having a nested value field is expected and aligns with the current implementation. Another argument was that it would be more convenient to have a flat structure without the need for a nested value field.

Ultimately, it was suggested to seek input from someone else, such as @gcanti, to make a decision on how to address this issue. Some key takeaways from the discussion include:

  • The current implementation of TaggedEnum requires a nested value field, which can result in unexpected never types.
  • There is a desire to improve the developer experience and avoid puzzling never errors at the construction call site.
  • Different approaches were proposed, including automatically adding a value field or using a different field name based on the tag name.
  • The trade-offs between convenience and maintaining consistency with the current implementation were discussed.

Ultimately, the decision on how to address this issue will depend on the goals and priorities of the Data.TaggedEnum library.

Discord thread

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

@Clindbergh
Copy link

@ahrjarrett / @tim-smart Is there now a way to create nested taggedEnums? E.g.

export type ServerErrors = Data.TaggedEnum<{
  BadGateway: {},
  GatewayTimeout: {
      timeout: number;
  }
}>

export type HttpError = Data.TaggedEnum<{
  NotFound: {};
  ServerError: ServerErrors;
}>;

@ahrjarrett
Copy link
Contributor

@Clindbergh It's been a minute since I worked on this, but IIRC the change implemented as part of #1521 was to show users a TypeError if they attempt to nest a TaggedEnum inside another TaggedEnum.

@tim-smart
Copy link
Member

This is how you would nest tagged enums:

import { Data } from "effect"

type ServerError = Data.TaggedEnum<{
  InternalError: { message: string }
}>

type HttpError = Data.TaggedEnum<{
  ServerError: { error: ServerError }
}>

const { InternalError } = Data.taggedEnum<ServerError>()
const { ServerError } = Data.taggedEnum<HttpError>()

ServerError({ error: InternalError({ message: "oops" }) })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants