Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Options APIの課題とComposition APIが解決したこと

Options APIの課題とComposition APIが解決したこと

Avatar for Yuta Inoue

Yuta Inoue

May 11, 2025

Other Decks in Programming

Transcript

  1. 課題3: TypeScriptとの相性が悪い Options APIでTypeScriptを使うと型定義が煩雑 推論・型安全が弱い(存在しないプロパティにアクセスしても型エラーにならない) interface ComponentData { user: {

    name: string } } interface ComponentMethods { updateUser(name: string): void } export default Vue.extend({ data(): ComponentData { return { user: { name: '' } } }, methods: { updateUser(newName: string) { this.user.name = newName this.user.age++ // 実行時にエラー } } }); 10
  2. 解決3: TypeScriptとの相性が良い Composition APIは標準的なTypeScript機能がそのまま使える import { ref } from 'vue'

    const user = ref({ name: '' }) const updateUser = (newName: string) => { user.value.name = newName user.value.age++ // 型エラーになる } 11