Running into Typescript complaints when trying to create a plugin. #600
-
I'm trying to create a plugin that allows you to specify a new option declare module 'pinia' {
export interface PiniaCustomProperties<
Id extends string = string,
S extends StateTree = StateTree,
G extends GettersTree<S> = GettersTree<S>,
A = ActionsTree
> {
isInitialized?: boolean;
$options: {
id: Id;
state?: () => S;
getters?: G;
actions?: A;
__initialize?: () => void | Promise<void>;
};
}
} This made it so that there were no complaints about accessing export interface DefineStoreOptions<Id extends string, S extends StateTree, G extends GettersTree<S>, A> {
id: Id;
state?: () => S;
getters?: G & ThisType<UnwrapRef<StateTree extends S ? {} : S> & StoreWithGetters<G> & PiniaCustomProperties>;
actions?: A &
ThisType<
A &
UnwrapRef<StateTree extends S ? {} : S> &
StoreWithState<Id, S, G, A> &
StoreWithGetters<GettersTree<S> extends G ? {} : G> &
PiniaCustomProperties
>;
__initialize?: () => void | Promise<void>;
} Now, in VS Code, I'm not seeing any errors, squiggly underlines, etc., but when I try to run it (using Vue CLI's
I just started using TypeScript a few days ago and what I'm WRITING is kinda over my head, but this error is really over my head. Any idea how to fix this? And since you're here, do you have a better idea than doing a plugin like this for handling "constructors"? I've been just creating an "initialize" action and making sure to call it right away after |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The updated docs are not published yet (it will with Vue 3.2.0), but you need to provide all generics with the exact same name as in the source code. You don't need to provide the default values. There is a full example at https://github.com/posva/pinia/blob/v2/test-dts/customizations.test-d.ts. I plan on creating a few small plugins as examples as well. BTW, you shouldn't redeclare existing options like actions, state, etc In your case something like this should do import 'pinia'
declare module 'pinia' {
export interface PiniaCustomProperties<Id, S, G, A> {
// NOTE: I would personally not bother with declaring this option and ignore it in ts since it's an internal property that is not used outside of `__initialize`
isInitialized?: boolean;
}
export interface DefineStoreOptions<Id, S, G A> {
__initialize?: () => Promise<void> | void
}
} About the constructor question:
|
Beta Was this translation helpful? Give feedback.
The updated docs are not published yet (it will with Vue 3.2.0), but you need to provide all generics with the exact same name as in the source code. You don't need to provide the default values. There is a full example at https://github.com/posva/pinia/blob/v2/test-dts/customizations.test-d.ts. I plan on creating a few small plugins as examples as well.
BTW, you shouldn't redeclare existing options like actions, state, etc
In your case something like this should do