Skip to content

Instantly share code, notes, and snippets.

@victor141516
Created May 26, 2025 13:53
Show Gist options
  • Save victor141516/893484d6750b1b368f885f907cf431c9 to your computer and use it in GitHub Desktop.
Save victor141516/893484d6750b1b368f885f907cf431c9 to your computer and use it in GitHub Desktop.
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