How to merge an interface and default values of a variable?

0

Issue

I have the disgusting code below where I want to combine the safety of TypeScript typing and default values of a variable:

interface Store {
  infinoteToken: string | null,
  allNotes: Note[],
  // plenty more
}

export const store: Store = reactive({
  infinoteToken: null,
  allNotes: [],
  // plenty more
})

I end up with two blocks of code with duplicated information.

Is there a way to combine these two elements to declare an Object with default values and types for its members?

Solution

This all can be moved to a class with default implementation:

class Store {
  infinoteToken: string | null = null;
  allNotes: Note[] = [];
  // plenty more

}

export const store = reactive(new Store());

Answered By – Dima Parzhitsky

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More