Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript + Vue3 in Practice

TypeScript + Vue3 in Practice

Vue 3 has excellent support for TypeScript; this tutorial introduces development practices for Vue 3 + TypeScript.

Vue 3's Composition API combines perfectly with TypeScript's type system, making code safer and easier to maintain.

View the Vue3 tutorial:https://www.runoob.com/vue3/vue3-tutorial.html


Vue 3 + TypeScript type support Vue 3 components defineComponent() PropType emits: ['edit', 'delete'] Composition API ref(initial) computed(() => {}) reactive(obj) Type definitions interface User { id: number name: string } Advantages of Vue 3 + TypeScript Complete type inference Props type checking Emits event types

Why use TypeScript in Vue 3

Vue 3 has fully embraced TypeScript from the outset, providing excellent type support.

The functional style of the Composition API works perfectly with the TypeScript type system: ref supports generics, computed has type inference, and props have complete type checking.

Using TypeScript gives Vue components type protection for their properties, events, and reactive data.

Vue 3 native support:Vue 3's source code is written in TypeScript and integrates seamlessly with TypeScript.


Create project

Use Vite to create a Vue 3 project with TypeScript support.

Initialization

Use Vite to create a Vue

3

TypeScript project (recommended)

npm create vite

@

latest my

vue

app

--

--

template vue

ts

Enter the project directory

cd my

vue

app

Install dependencies

npm install

Recommended:Vite is the officially recommended build tool for Vue; when creating a project, simply choose the vue-ts template.


Component types

Use defineComponent and PropType to define components and Props types.

src/components/UserCard.vue

<

template

<

div

class

=

"user-card"

<

h3

{

{

user.

name

}

}

h3

   

<p{{>

user.

email

}

}

p

   

<

button

@

click

=

"handleEdit"

Editing

button

div

template

<

script lang

=

"ts"

import

{

defineComponent

,

PropType

}

from

'vue'

// Define a user type interface

interface

User

{

  id

:

number

  name

:

string

  email

:

string

}

export

default

defineComponent

(

{

  name

:

'UserCard'

,

 

// Props definition

  props

:

{

   

// Use PropType for type declaration

    user

:

{

      type

:

Object

as PropType

<

User

,

required

:

true

   

}

 

}

,

 

// Declare events emitted by the component

  emits

:

[

'edit'

]

,

  setup

(

props

,

{

emit

}

)

{

   

// Handle edit click

   

const

handleEdit

=

(

)

=>

{

      emit

(

'edit'

,

props.

user

)

   

}

   

return

{

handleEdit

}

 

}

}

)

script

Running result:

UserCard 组件渲染成功

PropType:When a prop's type is a complex object, you need to use PropType for a type assertion.


Composition API types

When using reactive APIs such as ref and computed, TypeScript provides complete type support.

src/components/Counter.vue

<

template

<

div

<

p

Count

:

{

{

count

}

}

p

   

<

p

Doubling

:

{

{

doubled

}

}

p

   

<

button

@

click

=

"increment"

1

button

<

button

@

click

=

"decrement"

1

button

div

template

<

script lang

=

"ts"

import

{

ref

,

computed

}

from

'vue'

export

default

{

  setup

(

)

{

   

// ref: defines reactive data, with generics specifying the type

   

const

count

=

ref

<

number

(

0

)

// computed: a computed property, whose return type is automatically inferred

   

const

doubled

=

computed

(

(

)

=>

count.

value

2

)

   

// defines a method

   

const

increment

=

(

)

=>

{

      count.

value

++

   

}

   

const

decrement

=

(

)

=>

{

      count.

value

--

   

}

   

return

{

count

,

doubled

,

increment

,

decrement

}

 

}

}

script

Running result:

Counter 组件渲染成功

Type inference:Vue 3's Composition API automatically infers types based on initial values; for complex types, generics can be specified explicitly.


Interface definition

Define the project's type interfaces centrally in the types directory.

src/types/index.ts

// User type definition

export

interface

User

{

  id

:

number      

// The user ID

  name

:

string    

// Username

  email

:

string  

// Email

  avatar

?:

string

// Avatar (optional)

}

// Data transfer object for creating a user

export

interface

CreateUserDTO

{

  name

:

string    

// Username

  email

:

string  

// Email

  password

:

string

// Password

}

// Common API response type

export

interface

ApiResponse

<

T

{

success

:

boolean

 

// Whether it is successful

  data

?:

T        

// Data on success

  error

?:

string  

// Error message on failure

}

Centralized type management:Place type definitions in the types directory for easy reuse across multiple components.


Notes

Best practices:Vue 3 recommends using the Composition API + TypeScript for the best development experience.


Summary

Vue 3 combines perfectly with TypeScript.

Note:Using TypeScript is strongly recommended for Vue 3 projects, especially in large applications.