easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

type system is implemented backwards

Open factoidforrest opened this issue 2 years ago • 1 comments

Instead of making the user define the types that the store will implement, why don't you just have the compiler figure out the typing from the implimentation itself? That would greatly reduce boilerplate, which is a goal of this library.

createStore<StoreModel>({ doesn't need to take a generic, it can figure it out itself if you write the typescript definition right.

factoidforrest avatar Sep 11 '21 20:09 factoidforrest

I had considered this, but you might not have all the necessary typing available. Take for instance the following case;

createStore({
  session: null,
})

The type inference will be;

type StoreModel = {
  session: null;
}

Where really you were likely intending;

type StoreModel = {
  session: Session | null;
}

There could be an opportunity to use inference in the case that no model was provided though, as a better fallback, but IMO it will be brittle to optional / union field cases.

ctrlplusb avatar Oct 13 '21 14:10 ctrlplusb