Closed HerbCaudill closed 1 year ago
Yikes -- that seems like not enough types to me. Do your repos only have a single document type? In our past projects we've basically had a type per react component.
Yikes -- that seems like not enough types to me. Do your repos only have a single document type?
Well, in a typical React/Redux app, you'd have have several types but a single state type that structures it all together in a single tree, e.g.
interface State {
todos: Todo[]
visiblityFilter: 'ALL' | 'ACTIVE' | 'COMPLETED'
user: User
isNetworkOnline: boolean
}
In our past projects we've basically had a type per react component.
In that case you can either not pass a type - I've set the default to any
:
const repo = new Repo(...) // equivalent to `new Repo<any>(...)
or make a union type:
type ComponentState = CardState | AvatarState | DropdownState | PanelState
const repo = new Repo<ComponentState>({ storage })
Then you'd just cast the doc more specifically as needed.
repo.doc(id, (state: CardState) => {
// `state` has type `CardState`
}
This PR replaces
automerge.d.ts
with the type definitions I've proposed for automerge here: https://github.com/automerge/automerge/pull/155This type system differs from the existing one in that there is no
Doc<T>
, there is onlyT
.It also differs in that an entire
Repo
- and pretty much anything that pertains to that Repo - is bound to thatT
. So you'd do something like this:Note that you don't need to specify
repo.doc<MyState>(...)
because anything coming out of that repo will be assumed to be of the same type.