Vue.js course ยท Module 15: TypeScript & Nuxt
TypeScript with Vue.js
In this lesson5
Welcome to the NOVA LAB Quantum Laboratory! Year 2087 - the space station systems demand the highest precision. Any error in a data type could lead to a catastrophe. That's why our engineers use TypeScript - a static type system that catches errors before the code even runs.
Why TypeScript with Vue?
TypeScript is a superset of JavaScript that adds static types. Combined with Vue 3, it gives us:
- Autocompletion in the code editor (IntelliSense)
- Error detection before running the application
- Better documentation - types describe the shape of data
- Refactoring - the editor understands the code structure
Setting Up a Vue Project with TypeScript
Creating a Vue project with TypeScript is simple thanks to Vite:
npm create vite@latest nova-lab -- --template vue-tsVite automatically configures TypeScript and creates a tsconfig.json file:
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"jsx": "preserve",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.vue"]
}The lang="ts" Attribute
In Vue SFC (Single File Components), we add lang="ts" to the <script setup> block:
<script setup lang="ts">
import { ref } from 'vue'
const stationName = ref<string>('NOVA LAB')
const crewCount = ref<number>(42)
const isOnline = ref<boolean>(true)
</script>Without lang="ts", Vue treats the code as plain JavaScript. With lang="ts", the TypeScript compiler checks types and catches errors.
Basic Types in Vue
<script setup lang="ts">
import { ref } from 'vue'
// Simple types
const name: string = 'Mars Station'
const year: number = 2087
const active: boolean = true
// Arrays
const modules: string[] = ['Lab', 'Hangar', 'Bridge']
const temperatures: number[] = [-63, -58, -71]
// Ref with generic type
const status = ref<string>('operational')
const energy = ref<number>(98.5)
</script>TypeScript automatically infers types, so you don't always have to specify them explicitly:
const count = ref(0) // ref<number> - inferred automatically
const label = ref('test') // ref<string> - inferred automaticallyExplicit typing is useful when the type cannot be inferred:
const data = ref<string | null>(null) // can be string or nullTypeScript Interfaces
Interfaces define the shape of objects:
interface CrewMember {
id: number
name: string
role: string
active: boolean
}
const engineer: CrewMember = {
id: 1,
name: 'Dr. Nova',
role: 'Chief Engineer',
active: true
}Code for this lesson: App.vue
<script setup lang="ts">
import { ref } from 'vue'
// Basic types
const stationName = ref<string>('NOVA LAB')
const crewCount = ref<number>(42)
const isOnline = ref<boolean>(true)
// Interface
interface CrewMember {
id: number
name: string
role: string
active: boolean
}
// Typed array
const crew = ref<CrewMember[]>([
{ id: 1, name: 'Dr. Nova', role: 'Commander', active: true },
{ id: 2, name: 'Atlas', role: 'Navigator', active: true },
{ id: 3, name: 'Echo', role: 'Engineer', active: false }
])
// Union type
const status = ref<'online' | 'offline' | 'maintenance'>('online')
function toggleStatus() {
const states: Array<'online' | 'offline' | 'maintenance'> = ['online', 'offline', 'maintenance']
const idx = states.indexOf(status.value)
status.value = states[(idx + 1) % states.length]
}
</script>
<template>
<div class="nova-lab">
<h1>{{ stationName }}</h1>
<p>Crew: {{ crewCount }} | Status: <span :class="status">{{ status }}</span></p>
<button @click="toggleStatus" class="btn">Toggle status</button>
<h2>Crew</h2>
<div v-for="member in crew" :key="member.id" class="card">
<strong>{{ member.name }}</strong> - {{ member.role }}
<span :class="member.active ? 'active' : 'inactive'">
{{ member.active ? 'Active' : 'Inactive' }}
</span>
</div>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #00ff88;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 5px; }
h2 { color: #00b4d8; margin-top: 20px; }
.btn {
background: #00b4d8;
color: white;
border: none;
padding: 8px 16px;
border-radius: 8px;
cursor: pointer;
margin: 10px 0;
}
.card {
background: rgba(0, 180, 216, 0.1);
border: 1px solid #00b4d8;
padding: 12px 16px;
margin: 6px 0;
border-radius: 8px;
display: flex;
justify-content: space-between;
align-items: center;
}
.online { color: #00ff88; }
.offline { color: #e74c3c; }
.maintenance { color: #f39c12; }
.active { color: #00ff88; font-size: 12px; }
.inactive { color: #e74c3c; font-size: 12px; }
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is the main benefit of using TypeScript with Vue.js?
2. What attribute do we add to the <script setup> block to enable TypeScript?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Type ref as string, number, boolean and create an interface for a crew object.
- Click in order
Arrange the steps for configuring a Vue project with TypeScript:
- Code editor
Define a ModuleProps interface and use defineProps with a generic type.