在Vue中使用TypeScript的幾種方式
更新時間:2026年05月26日 10:11:38 作者:穆金秋
本文主要介紹了在Vue中使用TypeScript的幾種方式,包括通過<script lang="ts">結合defineComponent,使用Composition API + <script setup lang="ts">和TypeScript定義Props和Emits類型,下面就來詳細的介紹一下
Vue中使用TypeScript的幾種方式:
- 通過<script lang="ts">結合defineComponent;
- 推薦使用Composition API + <script setup lang="ts">實現類型推斷;
- 使用TypeScript定義Props和Emits類型。
配置步驟包括安裝依賴、設置tsconfig.json和env.d.ts文件。
使用TypeScript能提供類型安全、更好的IDE支持、清晰的接口定義和更高的代碼可維護性。
主流開發(fā)工具如VSCode、WebStorm、Vite和Vue CLI都提供良好支持。
在 Vue 中使用 TypeScript 的幾種方式
1.使用<script lang="ts">
<template>
<div>{{ message }}</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
data() {
return {
message: 'Hello TypeScript!' as string
}
},
methods: {
greet(name: string): string {
return `Hello, ${name}!`
}
}
})
</script>2.使用 Composition API +<script setup lang="ts">(推薦)
<template>
<div>{{ message }}</div>
<button @click="increment">Count: {{ count }}</button>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
// 類型推斷
const count = ref(0) // 自動推斷為 Ref<number>
const message = ref('Hello') // 自動推斷為 Ref<string>
// 顯式類型聲明
const user = ref<{ name: string; age: number }>({
name: 'John',
age: 25
})
// 類型化的函數
const increment = (amount: number = 1): void => {
count.value += amount
}
// 計算屬性自動推斷類型
const doubleCount = computed(() => count.value * 2)
</script>3.使用 TypeScript 定義 Props
<template>
<div>{{ title }} - {{ count }}</div>
</template>
<script setup lang="ts">
interface Props {
title: string
count?: number
items: string[]
}
const props = withDefaults(defineProps<Props>(), {
count: 0
})
</script>4.類型化的 Emits
<script setup lang="ts">
// 定義 emits 的類型
const emit = defineEmits<{
(e: 'update', value: number): void
(e: 'submit', data: { name: string; age: number }): void
}>()
const handleClick = () => {
emit('update', 10)
emit('submit', { name: 'John', age: 25 })
}
</script>配置步驟
1.安裝必要的依賴
npm install -D typescript @vue/tsconfig @vue/runtime-dom # 或 yarn add -D typescript @vue/tsconfig @vue/runtime-dom
2.創(chuàng)建/更新 tsconfig.json
{
"extends": "@vue/tsconfig/tsconfig.dom.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"exclude": ["src/**/__tests__/*"],
"compilerOptions": {
"composite": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}3.創(chuàng)建 env.d.ts 文件
/// <reference types="vite/client" />
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}使用 TypeScript 的好處
- 類型安全:在編譯時捕獲類型錯誤
- 更好的 IDE 支持:自動補全、類型提示、重構
- 清晰的接口定義:明確的 Props 和 Emits 類型
- 代碼可維護性:類型作為文檔,提高代碼可讀性
常見工具支持
- VSCode:配合 Volar 插件
- WebStorm:內置支持
- Vite:開箱即用
- Vue CLI:通過
vue add typescript
到此這篇關于在Vue中使用TypeScript的幾種方式的文章就介紹到這了,更多相關Vue使用TypeScript內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue 百度地圖(vue-baidu-map)繪制方向箭頭折線實例代碼詳解
這篇文章主要介紹了vue 百度地圖(vue-baidu-map)繪制方向箭頭折線,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
vue使用event.dataTransfer實現A容器數據拖拽復制到B容器方式
這篇文章主要介紹了vue使用event.dataTransfer實現A容器數據拖拽復制到B容器方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2026-02-02

