えぐろぐ

https://twitter.com/eggpogg

Nuxt x TypeScriptでStoreで自作プラグインの使う方法

this.$zisakuに返す型を定義して、interface Storeに渡してあげれば良い

// plugins/zisaku.ts

import { Plugin } from '@nuxt/types'

export interface Zisaku {
  hoge: () => void
  fuga: string
}

const zisaku = {
  hoge: () => { console.log('hoge') },
  fuga: 'fuga'
} as Zisaku

const plugin: Plugin = (context, inject) => inject('$zisaku', zisaku)

export default plugin
// types/index.d.ts

import { Zisaku } from '~/plugins/zisaku'

declare module 'vuex/types/index' {
  interface Store<S> {
    $zisaku: Zisaku
  }
}