TypeScript 与组合式 API
为组件的 prop 标注类型
当使用<script setup>
时,这个defineProps()
宏函数支持从它的参数中推导类型:
<script setup lang="ts"> const props =defineProps ({ foo: {type : String, required: true }, bar: Number }) props.foo // string props.bar // number | undefined </script>
这被称之为“运行时声明”,因为传递给defineProps()
的参数会作为运行时的props
选项使用。
然而,通过泛型参数来定义 prop 的类型通常更直接:
<script setup lang="ts"> const props =defineProps <{ foo: string bar?: number }>() </script>
这被称之为“基于类型的声明”。编译器会尽可能地尝试根据类型参数推导出等价的运行时选项。在这种场景下,我们第二个例子中编译出的运行时选项和第一个是完全一致的。
基于类型的声明或者运行时声明都可以使用,但是你不能同时使用两者。
我们也可以将 prop 的类型移入一个单独的接口中:
<script setup lang="ts"> interface Props { foo: string bar?: number } const props =defineProps <Props>() </script>
语法限制
为了生成正确的运行时代码,传给defineProps()
的泛型参数必须是以下之一:
一个类型字面量:
defineProps<{ /*... */ }>()
对同一个文件中的一个接口或对象类型字面量的引用:
interface Props { /* ... */} defineProps<Props>()
接口或对象字面类型可以包含从其他文件导入的类型引用,但是,传递给defineProps
的泛型参数本身不能是一个导入的类型:
import { Props } from './other-file' // 不支持! defineProps<Props>()
这是因为 Vue 组件是单独编译的,编译器目前不会抓取导入的文件以分析源类型。这个限制可能会在未来的版本中被解除。
Prop 默认值
当使用基于类型的声明时,我们失去了对 prop 定义默认值的能力。这可以通过目前实验性的响应性语法糖来解决:
<script setup lang="ts"> interface Props { foo: string bar?: number } // 对 defineProps() 的响应性解构 // 默认值会被编译为等价的运行时选项 const { foo, bar = 100 } = defineProps<Props>() </script>
这个行为目前需要显式地选择开启。
不使用<script setup>
如果没有使用<script setup>
,那么为了开启 prop 的类型推导,必须使用defineComponent()
。传入setup()
的prop对象类型是从props选项中推导而来。
import { defineComponent } from 'vue' export defaultdefineComponent ({ props: { message: String },setup (props) { props.message // <-- 类型:string } })
为组件的 emit 标注类型
在<script setup>
中,emit
函数的类型标注可以通过运行时声明或类型声明进行:
<script setup lang="ts"> // 运行时声明 const emit =defineEmits (['change', 'update']) // 基于类型的声明 const emit =defineEmits <{ (e: 'change', id: number): void (e: 'update', value: string): void }>() </script>
这个类型参数应该是一个带调用签名的类型字面量。这个类型字面量的类型就是返回的emit
函数的类型。我们可以看到,基于类型的声明使我们可以对所触发事件的类型进行更细粒度的控制。
若没有使用<script setup lang="ts">
,defineComponent()
也可以根据emits选项推导暴露在setup
上下文中的emit()
函数的类型:
<script lang="ts"> import { defineComponent } from 'vue' export defaultdefineComponent ({ emits: ['change'],setup (props, { emit }) {emit ('change') // <-- 类型检查 / 自动补全 } }) </script>
为ref()
标注类型
ref会根据初始化时的值推导其类型:
import { ref } from 'vue' // 推导出的类型:Ref<number> const year =ref (2020) // => TS Error: Type 'string' is not assignable to type 'number'. year.value = '2020'
有时我们可能想为ref内的值指定一个更复杂的类型,可以通过使用Ref这个类型:
import { ref, Ref } from 'vue' const year: Ref<string | number> =ref ('2020') year.value = 2020 // 成功!
或者,在调用ref()
时传入一个泛型参数,来覆盖默认的推导行为:
// 得到的类型:Ref<string | number> const year =ref <string | number>('2020') year.value = 2020 // 成功!
如果你指定了一个泛型参数但没有初始值,那么最后得到的就将是一个包含undefined的联合类型:
// 推导得到的类型:Ref<number | undefined> const n =ref <number>()
为reactive()
标注类型
reactive()
也会隐式地从它的参数中推导类型:
import { reactive } from 'vue' // 推导得到的类型:{ title: string } const book =reactive ({ title: 'Vue 3 指引' })
要显式地标注一个reactive
property 的类型,我们可以使用接口:
import { reactive } from 'vue' interface Book { title: string year?: number } const book: Book =reactive ({ title: 'Vue 3 指引' })
不推荐使用reactive()
的泛型参数,因为处理了深层次ref解包的返回值与泛型参数的类型不同。
为computed()
标注类型
computed()
会从其计算函数的返回值上推导出类型:
import { ref, computed } from 'vue' const count = ref(0) // 推导得到的类型:ComputedRef<number> const double =computed (() => count.value * 2) // => TS Error: Property 'split' does not exist on type 'number' const result = double.value.split('')
你还可以通过泛型参数显式指定类型:
const double =computed <number>(() => { // 若返回值不是 number 类型则会报错 })
为事件处理器标注类型
在处理原生 DOM 事件时,应该为我们传递给事件处理器的参数正确地标注类型。让我们看一下这个例子:
<script setup lang="ts"> functionhandleChange (event) { // `event` 隐式地标注为 `any` 类型 console.log(event.target.value) } </script> <template> <input type="text"@change ="handleChange " /> </template>
没有类型标注时,这个event参数会隐式地标注为any类型。这也会在tsconfig.json中配置了"strict": true
或"noImplicitAny": true
时报出一个 TS 错误。因此,建议显式地为事件处理器的参数标注类型。此外,你可能需要显式地强制类型转换event上的 property:
functionhandleChange (event: Event) { console.log((event.targetas HTMLInputElement).value) }
为 provide/inject 标注类型
provide()
和inject()
通常会在不同的组件中运行。要正确地为注入的值标记类型,Vue 提供了一个InjectionKey接口,它是一个继承自Symbol的泛型类型,可以用来在提供者和消费者之间同步注入值的类型:
将 key 的类型放在一个单独的文件中,这样它就可以被多个组件导入 // keys.js import { InjectionKey } from 'vue' const key =Symbol() as InjectionKey<string>
// 在供给方组件中 import { provide } from 'vue' import { key } from './keys.js'provide (key, 'foo') // 若提供的是非字符串值会导致错误
// 注入方组件 import { inject } from 'vue' import { key } from './keys.js' const foo =inject (key) // foo 的类型:string | undefined
当使用字符串注入 key 时,注入值的类型是unknown,需要通过泛型参数显式声明:
const foo =inject <string>('foo') // 类型:string | undefined
注意:注入的值仍然可以是undefined
,因为无法保证提供者一定会在运行时 provide 这个值。
当提供了一个默认值后,这个undefined
类型就可以被移除:
const foo =inject <string>('foo', 'bar') // 类型:string
如果你确定该值将始终被提供,则还可以强制转换该值:
const foo =inject ('foo') as string
模板 ref
为模板 ref 标注类型
模板ref
需要通过一个显式指定的泛型参数和一个初始值null
来创建:
<script setup lang="ts"> import { ref, onMounted } from 'vue' const el =ref <HTMLInputElement | null>(null ) onMounted(() => { el.value?.focus() }) </script> <template> <inputref ="el" /> </template>
注意:为了严格的类型安全,有必要在访问el.value时使用可选链或类型守卫。这是因为直到组件被挂载前,这个 ref 的值都是初始的null
,并且在由于v-if
的行为将引用的元素卸载时,也可以被设置为null
。
为组件模板 ref 标注类型
有时,你可能需要为一个子组件添加一个模板ref
,以便调用它公开的方法。举个例子,我们有一个MyModal
子组件,它有一个打开模态框的方法:
<!-- MyModal.vue --> <script setup lang="ts"> import { ref } from 'vue' const isContentShown = ref(false) const open = () => (isContentShown.value = true) defineExpose({ open }) </script>
为了获取MyModal
的类型,我们首先需要通过typeof得到其类型,再使用 TypeScript 内置的InstanceType工具类型来获取其实例类型:
<!-- 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>
注意,如果你想在 TypeScript 文件而不是在Vue SFC中使用这种技巧,需要开启Volar的托管模式。