cefn / lauf

Monorepo for lauf packages.
MIT License
5 stars 0 forks source link

Simplify or document pattern for use of Immutable initialiser #166

Open cefn opened 2 years ago

cefn commented 2 years ago

It's easy to get into a situation where the Store RootState type is flagged as Immutable because that was inferred from the useStore initialiser like this...

export interface AppState {
  q: string;
  results: SearchResult[];
}

export const INITIAL_STATE = {
  q: "",
  results: [],
} as const;

const store = useStore(INITIAL_STATE);

This is easily solved but the error message is cryptic. The solution is to be explicit about the type of your Stored RootState to establish the non-Readonly RootState as canonical, (although of course an Immutable initialiser is accepted to initially populate the Store)...

const store = useStore<AppState>(INITIAL_STATE);

There may be some way to simplify this pattern, or to harmlessly infer always the non-Readonly version of any initialising object, since Readonly is then re-applied given the Immutable annotations of the Store interface and Readonly<Readonly> collapses to Readonly so once is enough...

https://github.com/cefn/lauf/blob/main/modules/store/src/types/store.ts#L55-L74