Typescript union type as state in Pinia #1326
Replies: 3 comments 8 replies
-
May I ask why you are constructing so many different types if aren't using them? Wouldn't it be better to: enum States {
"loggingIn"="loggingIn",
"loggedIn"="loggedIn",
"loggedOut"="loggedOut",
"error"="error",
}
type AuthState = {
type: States,
user: User | null,
errorMsg: string | null,
} |
Beta Was this translation helpful? Give feedback.
-
You can actually use // or using $patch
this.$patch({ type: 'loggedIn', user: { id: '1' } }) Whole example: interface User {
id: string
}
type State<T extends string> = { type: T }
type AuthStateLoggingIn = State<'loggingIn'>
type AuthStateLoggedIn = State<'loggedIn'> & { user: User }
type AuthStateError = State<'error'> & { errorMsg: string }
type AuhtStateLoggedOut = State<'loggedOut'>
export type AuthState =
| AuthStateLoggingIn
| AuthStateLoggedIn
| AuthStateError
| AuhtStateLoggedOut
import { acceptHMRUpdate, defineStore } from 'pinia'
const delay = (t: number) => new Promise((r) => setTimeout(r, t))
export const useWholeStore = defineStore('whole', {
state: (): AuthState => ({ type: 'loggedIn', user: { id: '1' } }),
actions: {
async login() {
try {
await delay(1000)
this.$patch({ type: 'loggedIn', user: { id: '1' } })
} catch (e) {
const error = e as Error
this.$patch({ type: 'error', errorMsg: error.message })
}
},
logout() {
this.$patch({ type: 'loggedOut' })
},
},
}) Otherwise, you need to do state: () => ({ authState: { type: 'loggedOut' } as AuthState }), |
Beta Was this translation helpful? Give feedback.
-
@posva code you posted is same as mine, and setting state works as expected. The problem is in getters. Getters are unable to access properties of concrete States. I get 'undefined' when trying to get 'errorMsg' from AuthStateError in a getter error: (state) => {
// should return errorMsg but returns undefined
return state.type === 'error' ? state.errorMsg : ''
}, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm wondering would it be possible to use Typescript union types such as
to model state? So far I was not able to get proper result because getters would return undefined for each property other than shared property type.
Beta Was this translation helpful? Give feedback.
All reactions