Skip to content

Store with array of compostables - TS interface error #2842

Closed Answered by gnom1gnom
gnom1gnom asked this question in Help and Questions
Discussion options

You must be logged in to vote

The resolution I'be found is to start with the interface for the composable

export interface AnimalComposable {
  id: Ref<string>;
  name: Ref<string>;
  weight: Ref<number>;
}

Then, using UnwrapNestedRefs, I generate a type for the store items

export type Animal = UnwrapNestedRefs<AnimalComposable>;

With this the store definition looks as follows

export const useAnimalStore = defineStore('animalStore', () => {
  const animals = ref<Animal[]>([]);

  function get(animalId: string): Animal | undefined {
    return animals.value.find((animal) => animal.id === animalId);
  }

  function add(animal: Animal | AnimalComposable) {
    animals.value.push(isAnimalComposable(animal) ? reactive(animal)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by gnom1gnom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant