适合:菜单配置、状态文案表、颜色表、权限到按钮名的映射等。
<script setup lang="ts">
const STATUS_TEXT = {
idle: '空闲',
loading: '加载中',
success: '成功',
error: '失败',
} as const
type Status = keyof typeof STATUS_TEXT // 'idle' | 'loading' | 'success' | 'error'
</script>
<template>
<div>{{ STATUS_TEXT.loading }}</div>
</template>
这里 as const 的作用:让每个字段保持“字面量”,推导更精确。