<script setup lang="ts">
import { computed, ref } from "vue";
import { ExhaustiveError } from "./errors";
const cakeList = [
{ name: "ショートケーキ", fruit: "イチゴ" },
{ name: "チーズケーキ" },
] as const;
type Cake = (typeof cakeList)[number];
const myCake = ref<Cake>(cakeList[0]);
const message = computed(() => {
switch (myCake.value.name) {
case "ショートケーキ":
return `上は ${myCake.value.fruit} だ!`;
case "チーズケーキ":
return `チーズケーキおいしい!`;
default:
return new ExhaustiveError(myCake.value);
}
});
</script>
<template>
<select v-model="myCake">
<option v-for="cake in cakeList" :value="cake">
{{ cake.name }}
</option>
</select>
<h1>{{ message }}</h1>
</template>
-- src/errors.ts
export class ExhaustiveError extends Error {
constructor(
value: never,
message = `Unsupported type: ${value}`
) {
super(message);
}
}