Как реализовать, чтобы Typescript в Nuxt 2 не ругался

делаю авторизацию пользователя

сделал класс auth

import type { Context } from '@nuxt/types';

export class Auth {
  public ctx: Context;

  constructor(ctx: Context) {
    this.ctx = ctx;
  }

  async login(data: any): Promise<any> {
    return await this.ctx.app.$projectServices.repository.login(data);
  }

  async fetchUser() {
    return await this.ctx.app.$projectServices.repository.getAuthUser();
  }
}

создал плагин для инжекта глобальной переменной $auth

import { Plugin } from '@nuxt/types';

import { Auth } from '~/services/auth/auth';

const auth: Plugin = (context, inject) => {
  const auth = new Auth(context);

  inject('auth', auth);
};

export default auth;

при инициализации накста делаю проверку на наличие токена в localStorage и если есть, то делаю запрос на получение даннных пользователя

nuxtClientInit plugin

export default async function (context: any) {
  await context.store.dispatch('nuxtClientInit');
}

store

import { getToken } from '~/services/auth/token';

export const actions = {
  async nuxtClientInit({ commit }: any) {
    const token = getToken();
    if (token) {
      commit('auth/setToken', { token });
      await this.$auth
        .fetchUser()
        .then((response: any) => {
          commit('setItem', response);
        })
        .catch((e: any) => {
          if (e.status === 401) {
            commit('auth/clear');
          }
        });
    }
  },
};

в сторе ругается на this.$auth

TS2339: Property '$auth' does not exist on type '{ nuxtClientInit({ commit }: any): Promise ; }'

как решить проблему?


Ответы (0 шт):