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

extracting variant type #157

Open
harrysolovay opened this issue Apr 22, 2023 · 0 comments
Open

extracting variant type #157

harrysolovay opened this issue Apr 22, 2023 · 0 comments

Comments

@harrysolovay
Copy link
Contributor

Ideally one could extract the native types of specific members of tagged unions.

Let's say we want to extract the $capiBinary variant-specific type:

const $pathBinary = $.variant("path", $.str)
const $capiBinary = $.variant(
  "capi",
  $.object(
    $.field("name", $.str),
    $.optionalField("version", $.str),
  ),
)

type Binary = $.Input<typeof $binary>
const $binary = $.taggedUnion("type", [
  $pathBinary,
  $capiBinary,
])

Today, we would need to make use of awkward workarounds such as...

type PathBinary = Extract<Binary, { type: "capi" }>

Should we enable a more-direct means of getting this type? Perhaps a variant-specific type util?

type CapiBinary = $.VariantInput<typeof $binary, "path">

If we ultimately get rid of tag specificity, then we could support utilizing Input and Output on variants themselves (since we know the discriminant key is "type").

type CapiBinary = $.Input<typeof $capiBinary>
@tjjfvi tjjfvi added this to Capi Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant