history
history copied to clipboard
typing v5: Provide state type when creating history
👋
When creating an history using createBrowserHistory
or createMemoryHistory
, it could be great to be able to pass a type that will be pass as state instead of always having object | null
.
Here is what it could look like:
const history = createBrowserHistory<myHistoryStateType>()
const history = createMemoryHistory<myHistoryStateType>()
It will allow to do something like:
type myHistoryStateType = {
extra: string
}
const history = createBrowserHistory<myHistoryStateType>()
history.listen(({ location }) => {
const { state } = location
console.log(state.extra)
})
instead of currently have to force the type after:
type myHistoryStateType = {
extra: string
}
const history = createBrowserHistory()
history.listen(({ location }) => {
const state = location.state as HistoryStateType
console.log(state.extra)
})
This would make the workaround for #791 slightly less hackish. Currently I guess I'm gonna have to replace the initial location to attach state to mark the very first entry to prevent going back beyond it