How to use Typing new state in the state type defination? #788
-
Hello, Just wanted to ask that how am i supposed to use the type defined for state from https://pinia.esm.dev/core-concepts/plugins.html#typing-new-store-properties Sorry, a bit confused and lurked into docs but didn't found it use case. Any guidance please 🙏 @posva As a workaround I am doing something like this - import { defineStore } from 'pinia';
interface userData { email: string; } // and other properties for data
interface UserProperties {
name: string;
data: userData;
}
const userStore = defineStore({
id: 'user',
state: () =>
({
name: '',
data: {},
} as UserProperties),
}); Want to know how to achieve this from https://pinia.esm.dev/core-concepts/plugins.html#typing-new-store-properties |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The docs you linked are referring to plugins and don't seem related to what you're trying to accomplish here in defining a store. A slightly better way to accomplish what you're trying to do is the following: const userStore = defineStore({
id: 'user',
// type the return of state instead
state: (): UserProperties => ({
name: '',
data: {}, // this will now show a type error
}),
}); If you do this, according to your example above you will see a type error because you define |
Beta Was this translation helpful? Give feedback.
The docs you linked are referring to plugins and don't seem related to what you're trying to accomplish here in defining a store.
A slightly better way to accomplish what you're trying to do is the following:
If you do this, according to your example above you will see a type error because you define
email
as a property indata
but aren't providing it.