vue-match
<Match :value="state"> must have a slot for every variant of state's enum type (or #_ for the rest), and every slot must name a variant.
Why: a variant without a slot renders nothing, silently: the Failed state you forgot, or the variant someone adds next month. <Match> is match(...) for templates, and like match, it is checked. Guide: Vue.
✕ Rejectedvue-match
vue
<script setup lang="ts">
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import { Match } from "@evobug_com/safe-ts-vue/match";
type Payment = Enum<{ Pending: {}; Paid: { receipt: string }; Refunded: { reason: string } }>;
const props = defineProps<{ payment: Payment }>();
</script>
<template>
<Match :value="props.payment">
<template #Pending>Waiting for the bank</template>
<template #Paid="{ receipt }">Receipt {{ receipt }}</template>
</Match>
</template>A refunded payment shows nothing.
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import { Match } from "@evobug_com/safe-ts-vue/match";
type Payment = Enum<{ Pending: {}; Paid: { receipt: string }; Refunded: { reason: string } }>;
const props = defineProps<{ payment: Payment }>();
</script>
<template>
<Match :value="props.payment">
<template #Pending>Waiting for the bank</template>
<template #Paid="{ receipt }">Receipt {{ receipt }}</template>
<template #Refunded="{ reason }">Refunded: {{ reason }}</template>
</Match>
</template>#_ shows the variants without a slot of their own. Use it when they really are "everything else": unlike a slot per variant, it won't tell you when a variant is added.
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import { Match } from "@evobug_com/safe-ts-vue/match";
type Payment = Enum<{ Pending: {}; Paid: { receipt: string }; Refunded: { reason: string } }>;
const props = defineProps<{ payment: Payment }>();
</script>
<template>
<Match :value="props.payment">
<template #Paid="{ receipt }">Receipt {{ receipt }}</template>
<template #_>Not paid</template>
</Match>
</template>Also reported:
- a slot that isn't a variant (
#Refundd), with the variants it could be; #_before other slots (it goes last, like_inmatch);- content in
<Match>outside a<template #Variant>, which is never shown; :valuegiven a Ref instead of its value. The template only unwraps refs at the top level, soconst task = useTask(...)needs:value="task.state.value". The quick fix adds.value;- a value whose type the checker can't see, without
#_. It reads the type from the declaration:ref<Payment>(...),shallowRef(Payment.Pending()),shallowRef<Option<Payment>>(undefined)(with<Match>underv-if),defineProps<{ payment: Payment }>(),useTask(...), a function's return type,v-forover a typed list, a slot prop of a<Match>on an enum declared in the component (#Rejected="{ error }"is theRejectedvariant'serrorfield). Annotate the declaration, or add#_.
<Match> has no #undefined slot for a missing value, unlike match(...) in the script (Matching an Option): #undefined is reported as not a variant. Show the missing case with v-if / v-else around the <Match>.
:on is the older name of the prop, and the checker and the build read it the same. vue-tsc refuses it (TS2345: Vue's props type reads a prop named on as an event listener, which must be a function), so write :value. A <Match> with neither is reported.
The variants come from the Enum<{ ... }> declaration, in the component or in the file the type is imported from.