keyof typeof:对象值 → 它的 key 联合类型

7 阅读1分钟
const STATUS_TEXT = {
  idle: '空闲',
  loading: '加载中',
  success: '成功',
} as const

type StatusKey = keyof typeof STATUS_TEXT
// StatusKey = 'idle' | 'loading' | 'success'

// 用起来:只能填这些值
let s: StatusKey
s = 'idle'    // ✅
s = 'error'   // ❌ 报错(不在 key 联合里)