tjjfvi/subshape

extracting variant type

Opened this issue · 0 comments

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>