Types in plugins #578
-
Suppose I have a couple of types and store definitions: type AuthState = { accessToken: string; }
type UserState = { name: string }
type States = {
auth: AuthState;
user: UserState;
}
const useAuth = defineStore({ id: 'auth' });
const useUser = defineStore({ id: 'user' }); Initial state of the stores is populated by plugin. Here is the plugin: const localStoragePlugin = ({ store }: PiniaPluginContext) => {
const serialized = localStorage.getItem(store.$id);
if (serialized !== null) {
const state = <States[typeof store.$id]>JSON.parse(serialized); // <<<------ problem here
store.$state = state;
return state;
}
}; I want the types to be narrowed based on the Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't think you will be able to infer that because PiniaPluginContext is not aware of defined stores and making it aware would be a very inconvenient manual step anyway |
Beta Was this translation helpful? Give feedback.
I don't think you will be able to infer that because PiniaPluginContext is not aware of defined stores and making it aware would be a very inconvenient manual step anyway