lmiller1990/v-switch

Typescript ADT support

ohuu opened this issue · 3 comments

ohuu commented

I'm not sure if it's even possible but it would be awesome if we could somehow support typescript ADT's

for example:

type Searching = {
  _tag: "Searching";
}

type SearchFail = {
  _tag: "SearchFail";
  err: string;
}

type SearchSuccess = {
  _tag: "SearchSuccess";
  res: number;
}

type Search = Searching | SearchFail | SearchSuccess;
<template>
  <v-switch :case="search">
    <template #SearchingFail v-slot="[fail]">
      <span>{{ fail.err }}</span>
    </template>
    <template #SearchingSuccess v-slot="[success]">
      <span>{{ success.res }}</span>
    </template>
  </v-switch>
</template>

The above is idealised pseudocode but it would be awesome if we had a component like this.

What does "ADT" stand for?

I'm not sure what the limitations are, I suppose this would be something we achieve using an extension like Volar. It sounds like a great idea, if it's technical possible.

ohuu commented

ADT stands for Algebraic Data Type.

I see. I always thought of this pattern is a "discriminated union" example. I don't know too much about type theory, but if we can compile this to a typed switch statement using Volar, I think we should be able to implement this.

I don't know if I can work on this in the near future, but I'd really like to see this supported. I'm pretty sure it's already possible with the existing tooling, someone with knowledge of Volar will need to do some investigation.