-
hi,
This works, but typescript doesnt recognise the '.currentId' type in the current function. Would you have any idea how to resolve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi, that's a hard one. I think you are more advanced in typescript than me ^^.
import type { DefineStoreOptionsBase } from 'pinia'
import { defineStore } from 'pinia'
import { useStoreActions } from './useStoreActions'
export function useDataStore<S extends DataStoreState, T extends DataStore = DataStore>(
id: string,
options: DefineStoreOptionsBase<S, T>,
) {
return defineStore(id, {
state: () => ({ data: {} } as S),
actions: useStoreActions(),
...options,
})
}
export interface DataStoreState {
data: Record<string, any>
[s: string]: any
}
export type DataStore = ReturnType<typeof import('@/composables')['useDataStore']> and changing ...
piniaStore<S extends DataStoreState = DataStoreState>() {
return useDataStore<S>(this.model.$entity(), this.model.$piniaOptions())(this.pinia)
}
... Then you can use it like that: interface CurrentState<T extends MyModel = MyModel> {
data: Record<string, Record<string, T>>
currentId: number | string | null
}
export default class MyModel extends Model {
static piniaOptions = {
state: () => ({
data: {},
currentId: null,
}),
}
static current<T extends typeof VellumModel>(this: T): InstanceType<T> | null {
let repo = useRepo(this);
let currentId: number | string | null = repo.piniaStore<CurrentState<InstanceType<T>>>().currentId
return currentId ? repo.find(currentId) as InstanceType<T> : null;
}
} |
Beta Was this translation helpful? Give feedback.
Hi,
that's a hard one. I think you are more advanced in typescript than me ^^.
I tried now for some time making a solution.
I don't know if there is a way to do it better, but for now i found this solution which i can implement. What do you think about it?
composables/useDataStore.ts