Created
May 26, 2025 13:53
-
-
Save victor141516/893484d6750b1b368f885f907cf431c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readonly, ref } from 'vue' | |
import type { Ref } from 'vue' | |
type StoreItem<N extends string, T> = { | |
readonly [K in N]: Readonly<Ref<T>> | |
} & { | |
readonly [K in `set${Capitalize<N>}`]: (value: T) => void | |
} | |
export const createStoreItem = <N extends string, T>(name: N, initialValue: T) => { | |
const store = ref<T>(initialValue) | |
function setter(value: T) { | |
store.value = value | |
} | |
return { | |
[name]: readonly(store), | |
[`set${name.charAt(0).toLocaleUpperCase()}${name.slice(1)}`]: setter, | |
} as StoreItem<N, T> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment