Vue.js course Β· Module 15: TypeScript & Nuxt

Typing Props

2 min read Β·
In this lesson5

In the Quantum Laboratory, every station module must precisely declare what data it accepts. In Vue 3 with TypeScript, we use the generic syntax defineProps<{}>() to type props.

defineProps with Generic Type

Instead of runtime validation (object with type, required), we can use TypeScript syntax:

<script setup lang="ts">
// Generic syntax - types checked at compile time
const props = defineProps<{
  title: string
  count: number
  isActive: boolean
}>()
</script>

This is equivalent to the traditional syntax:

<script setup>
// Old syntax - runtime validation
const props = defineProps({
  title: { type: String, required: true },
  count: { type: Number, required: true },
  isActive: { type: Boolean, required: true }
})
</script>

Extracting an Interface

When a component has many props, it's worth extracting an interface:

<script setup lang="ts">
interface StationModuleProps {
  name: string
  status: 'online' | 'offline' | 'maintenance'
  powerLevel: number
  crew?: string[]  // optional prop (?)
}

const props = defineProps<StationModuleProps>()
</script>

The ? sign means the prop is optional - it doesn't have to be passed.

withDefaults

TypeScript generics don't allow default values in defineProps. That's what withDefaults is for:

<script setup lang="ts">
interface PanelProps {
  title: string
  variant?: 'primary' | 'danger' | 'success'
  maxItems?: number
  showHeader?: boolean
}

const props = withDefaults(defineProps<PanelProps>(), {
  variant: 'primary',
  maxItems: 10,
  showHeader: true
})
</script>

Now props.variant will be 'primary' if not provided.

Union Types in Props

TypeScript allows defining precise types:

<script setup lang="ts">
type AlertLevel = 'info' | 'warning' | 'critical'

interface AlertProps {
  level: AlertLevel
  message: string
  code?: number
}

const props = defineProps<AlertProps>()
</script>

<template>
  <div :class="['alert', props.level]">
    {{ props.message }}
  </div>
</template>

Complex Prop Types

Props can contain nested objects and arrays:

<script setup lang="ts">
interface Coordinate {
  x: number
  y: number
  z: number
}

interface SensorData {
  id: string
  location: Coordinate
  readings: number[]
  metadata?: Record<string, unknown>
}

interface SensorPanelProps {
  sensors: SensorData[]
  selectedId: string | null
  onSelect?: (id: string) => void
}

const props = defineProps<SensorPanelProps>()
</script>

Record<string, unknown> is a dictionary type - an object with string keys and values of any type.

Code for this lesson: App.vue
<script setup lang="ts">
import { ref } from 'vue'
import ModuleCard from './ModuleCard.vue'

interface StationModule {
  id: string
  name: string
  status: 'online' | 'offline' | 'maintenance'
  power: number
}

const modules = ref<StationModule[]>([
  { id: 'm1', name: 'Life Support', status: 'online', power: 95 },
  { id: 'm2', name: 'Navigation', status: 'online', power: 88 },
  { id: 'm3', name: 'Science Lab', status: 'maintenance', power: 40 },
  { id: 'm4', name: 'Communications', status: 'offline', power: 0 }
])
</script>

<template>
  <div class="nova-lab">
    <h1>NOVA LAB - Typed Props</h1>
    <ModuleCard
      v-for="mod in modules"
      :key="mod.id"
      :module="mod"
      :show-power="true"
    />
  </div>
</template>

<style scoped>
.nova-lab {
  background: #0a0e27;
  color: #00ff88;
  min-height: 100vh;
  padding: 20px;
  font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 15px; }
</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. 1. Which syntax defines typed props in Vue 3 with TypeScript?

  2. 2. How do you mark a prop as optional in a TypeScript interface?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Use withDefaults(defineProps<Props>(), { ... }) to set default values.

  • Horizontal ordering

    Arrange the correct withDefaults syntax with defineProps:

  • Code editor

    Use defineEmits<{}> to define a 'command' event with typed arguments.

  1. QuestionGeneric defineProps Syntax
  2. QuestionOptional Prop in TypeScript
  3. Coding taskPractice: defineProps with withDefaults
  4. Put in orderwithDefaults Syntax
  5. QuestionUnion Type in Props
  6. Coding taskPractice: Typed Emits

Useful articles