Fix typing 🤔

This commit is contained in:
Christian Cueni 2022-05-04 15:38:59 +02:00
parent cc29f36230
commit 2104c8daac
2 changed files with 11 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import type {NavigationGuardWithThis, RouteLocationNormalized} from 'vue-router'; import type {NavigationGuardWithThis, RouteLocationNormalized} from 'vue-router';
import { useUserStore } from '@/stores/user' import {useUserStore} from '@/stores/user'
import type {UserState} from '@/stores/user'
import type {Store} from 'pinia'; import type {Store} from 'pinia';
const cookieName = 'loginStatus' const cookieName = 'loginStatus'
@ -31,11 +32,11 @@ const getCookieValue = (cookieName: string): string => {
} }
// Pina is not ready when router is called the first time by app.use(), so we need to load it here // Pina is not ready when router is called the first time by app.use(), so we need to load it here
const getUserStore = (): Store => { const getUserStore = (): UserState & Store => {
if (!userStore) { if (!userStore) {
userStore = useUserStore() userStore = useUserStore()
} }
return userStore return userStore as unknown as UserState & Store
} }
const loginRequired = (to: RouteLocationNormalized) => { const loginRequired = (to: RouteLocationNormalized) => {

View File

@ -1,11 +1,17 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
// typed state https://stackoverflow.com/questions/71012513/when-using-pinia-and-typescript-how-do-you-use-an-action-to-set-the-state // typed state https://stackoverflow.com/questions/71012513/when-using-pinia-and-typescript-how-do-you-use-an-action-to-set-the-state
export type UserState = {
email: string;
loggedIn: boolean;
}
export const useUserStore = defineStore({ export const useUserStore = defineStore({
id: 'user', id: 'user',
state: () => ({ state: () => ({
email: '', email: '',
loggedIn: false loggedIn: false
}), } as UserState),
getters: { getters: {
}, },
actions: { actions: {