Vue in Vue is best learned by connecting the rule to a small admin screen. Start with the smallest component or composable, observe the output, and then add one realistic constraint so the concept becomes practical.
The key habit for this lesson is to watch ref, prop, slot, or emitted event as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.
Vue 3 was rewritten in TypeScript and has first-class TypeScript support. Using TypeScript with Vue gives you:
<!-- TypedComponent.vue -->
<template>
<div>
<h2>{{ user.name }}</h2>
<p>{{ user.email }}</p>
<span :class="`badge-${user.role}`">{{ user.role }}</span>
<button @click="handleEdit">Edit</button>
<p>Count: {{ count }}</p>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import type { User, UserRole } from '@/types'
// Typed props with defineProps
interface Props {
user: User
editable?: boolean
maxItems?: number
}
const props = withDefaults(defineProps<Props>(), {
editable: true,
maxItems: 10,
})
// Typed emits with defineEmits
interface Emits {
(e: 'edit', user: User): void
(e: 'delete', id: number): void
(e: 'update:user', user: User): void
}
const emit = defineEmits<Emits>()
// Typed refs
const count = ref<number>(0)
const name = ref<string>('')
const users = ref<User[]>([])
const selectedRole = ref<UserRole | null>(null)
// Typed computed
const isAdmin = computed<boolean>(() => props.user.role === 'admin')
const displayName = computed<string>(() => `${props.user.name} (${props.user.role})`)
// Typed function
function handleEdit(): void {
emit('edit', props.user)
}
function updateUser(updates: Partial<User>): void {
emit('update:user', { ...props.user, ...updates })
}
// Typed template ref
import { useTemplateRef } from 'vue'
const inputRef = useTemplateRef<HTMLInputElement>('myInput')
function focusInput(): void {
inputRef.value?.focus()
}
</script>
// composables/useFetch.ts - typed composable
import { ref, watchEffect, toValue, type MaybeRefOrGetter } from 'vue'
interface FetchState<T> {
data: T | null
loading: boolean
error: string | null
}
export function useFetch<T>(url: MaybeRefOrGetter<string>) {
const state = ref<FetchState<T>>({
data: null,
loading: false,
error: null,
})
watchEffect(async () => {
const resolvedUrl = toValue(url)
if (!resolvedUrl) return
state.value.loading = true
state.value.error = null
try {
const res = await fetch(resolvedUrl)
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`)
state.value.data = await res.json() as T
} catch (err) {
state.value.error = err instanceof Error ? err.message : 'Unknown error'
} finally {
state.value.loading = false
}
})
return state
}
// Usage with type inference:
// const usersState = useFetch<User[]>('/api/users')
// usersState.value.data // typed as User[] | null
// usersState.value.loading // boolean
// usersState.value.error // string | null
// composables/useCounter.ts
import { ref, computed } from 'vue'
export function useCounter(initialValue: number = 0) {
const count = ref(initialValue)
const doubled = computed(() => count.value * 2)
const isPositive = computed(() => count.value > 0)
function increment(by: number = 1): void { count.value += by }
function decrement(by: number = 1): void { count.value -= by }
function reset(): void { count.value = initialValue }
return { count, doubled, isPositive, increment, decrement, reset }
}
// types/index.ts - shared type definitions
export type UserRole = 'admin' | 'user' | 'moderator' | 'guest'
export interface User {
id: number
name: string
email: string
role: UserRole
avatar?: string
createdAt: Date
isActive: boolean
}
export interface ApiResponse<T> {
data: T
message: string
success: boolean
pagination?: {
page: number
perPage: number
total: number
}
}
export interface FormField {
value: string
error: string | null
touched: boolean
}
export type FormState<T extends Record<string, unknown>> = {
[K in keyof T]: FormField
}
// Utility types
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
export type CreateUser = Optional<User, 'id' | 'createdAt' | 'isActive'>
export type UpdateUser = Partial<Omit<User, 'id'>>
// Component prop types
export interface TableColumn<T = unknown> {
key: keyof T
label: string
sortable?: boolean
width?: string
align?: 'left' | 'center' | 'right'
formatter?: (value: T[keyof T], row: T) => string
}
Understanding Typescript is not just about syntax. In production applications, this topic directly affects maintainability, debugging speed, and team collaboration. Focus on readability, small reusable patterns, and predictable state flow when implementing Typescript.
A practical approach is to first implement the simplest working version, then refactor into reusable pieces (components/composables/stores) only when duplication appears. This helps keep your Vue codebase clean while avoiding over-engineering.
Use Vue when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real Vue task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.
A reliable practice flow is: create the smallest working component or composable, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with Vue Devtools and template warnings. If the result surprises you, reduce the code until the behavior is visible again.
The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why Vue is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.
Copying the syntax before understanding the behavior.
Write the expected behavior first, then make the example prove it.
Practicing only the perfect input.
Also test missing, repeated, empty, or boundary input before considering the lesson complete.
Looking only at the final output.
Trace ref, prop, slot, or emitted event through each important step.
Use it when the problem matches the behavior shown in the example and when the result can be verified through Vue Devtools and template warnings.
Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.
Trace ref, prop, slot, or emitted event, predict the result, run the example, and compare your prediction with the actual output.
Explore 500+ free tutorials across 20+ languages and frameworks.