ts+vue实践

45 阅读1分钟

cn.vuejs.org/guide/types…

defineEmits

const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
}>()

computed

const double = computed<number>(() => {
  // 若返回值不是 number 类型则会报错
})

事件处理函数

function handleChange(event: Event) {
  console.log((event.target as HTMLInputElement).value)
}

provide / inject

import { provide, inject } from 'vue'
import type { InjectionKey } from 'vue'

const key = Symbol() as InjectionKey<string>

provide(key, 'foo') // 若提供的是非字符串值会导致错误

const foo = inject(key) // foo 的类型:string | undefined