为 ref
,reactive
,computed
标注类型
对于
ref
,computed
可以直接传入泛型参数,不推荐使用reactive()
的泛型参数,因为处理了深层次 ref 解包的返回值与泛型参数的类型不同。
import { reactive } from 'vue'
interface Book {
title: string
year?: number
}
const book: Book = reactive({ title: 'Vue 3 指引' })
为组件的 props 标注类型
基于类型的声明或者运行时声明可以择一使用,但是不能同时使用。
<script setup lang="ts">
interface Props {
foo: string
bar?: number
}
const props = defineProps<Props>()
</script>
当使用基于类型的声明时,我们失去了为 props 声明默认值的能力。这可以通过 withDefaults
编译器宏解决:
export interface Props {
msg?: string
labels?: string[]
}
const props = withDefaults(defineProps<Props>(), {
msg: 'hello',
labels: () => ['one', 'two']
})
为组件的 emits 标注类型
<script setup lang="ts">
// 3.3+: 可选的、更简洁的语法
const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>()
</script>
为事件处理函数标注类型
显式地为事件处理函数的参数标注类型。此外,你在访问 event
上的属性时可能需要使用类型断言
function handleChange(event: Event) {
console.log((event.target as HTMLInputElement).value)
}
为模板引用标注类型
模板引用需要通过一个显式指定的泛型参数和一个初始值 null
来创建
<script setup lang="ts">
import { ref, onMounted } from 'vue'
const el = ref<HTMLInputElement | null>(null)
onMounted(() => {
el.value?.focus()
})
</script>
<template>
<input ref="el" />
</template>
注意为了严格的类型安全,有必要在访问 el.value
时使用可选链或类型守卫。这是因为直到组件被挂载前,这个 ref 的值都是初始的 null
,并且在由于 v-if
的行为将引用的元素卸载时也可以被设置为 null
。
为组件模板引用标注类型
<!-- MyModal.vue -->
<script setup lang="ts">
import { ref } from 'vue'
const isContentShown = ref(false)
const open = () => (isContentShown.value = true)
defineExpose({
open
})
</script>
<!-- App.vue -->
<script setup lang="ts">
import MyModal from './MyModal.vue'
const modal = ref<InstanceType<typeof MyModal> | null>(null)
const openModal = () => {
modal.value?.open()
}
</script>
或者直接写成
import { ref } from 'vue'
import type { ComponentPublicInstance } from 'vue'
const child = ref<ComponentPublicInstance | null>(null)