History enhancers definitions improved using union types

This commit is contained in:
Sergey Buturlakin
2015-10-01 12:31:03 +03:00
parent f0d5440f90
commit 0d64ea1b82
2 changed files with 52 additions and 37 deletions

View File

@@ -12,12 +12,13 @@ declare namespace HistoryModule {
type BeforeUnloadHook = () => string
type CreateHistory = (options?: HistoryOptions) => History
type CreateHistory<T> = (options?: HistoryOptions) => T
type CreateHistoryEnhancer = (createHistory: CreateHistory) => CreateHistory
type CreateHistoryEnhancer<T, E> = (createHistory: CreateHistory<T>) => CreateHistory<T & E>
interface HistoryBase {
interface History {
listenBefore(hook: TransitionHook): Function
listen(listener: LocationListener): Function
transitionTo(location: Location): void
pushState(state: LocationState, path: Path): void
replaceState(state: LocationState, path: Path): void
@@ -30,10 +31,6 @@ declare namespace HistoryModule {
createHref(path: Path): Href
}
interface History {
listen(listener: LocationListener): Function
}
type HistoryOptions = Object
type Href = string
@@ -63,17 +60,30 @@ declare namespace HistoryModule {
type TransitionHook = (location: Location, callback: Function) => any
interface HistoryBeforeUnload {
listenBeforeUnload(hook: () => string | boolean): Function
}
interface HistoryQueries {
pushState(state: LocationState, pathname: Pathname | Path, query?: Query): void
replaceState(state: LocationState, pathname: Pathname | Path, query?: Query): void
createPath(path: Path, query?: Query): Path
createHref(path: Path, query?: Query): Href
}
// Global usage, without modules, needs the small trick, because lib.d.ts
// already has `history` and `History` global definitions:
// var createHistory = ((window as any).History as HistoryModule.Module).createHistory;
interface Module {
createHistory: CreateHistory
createHashHistory: CreateHistory
createMemoryHistory: CreateHistory
createHistory: CreateHistory<History>
createHashHistory: CreateHistory<History>
createMemoryHistory: CreateHistory<History>
createLocation(): Location
useBasename(enhancer: CreateHistoryEnhancer): CreateHistory
useBeforeUnload(enhancer: CreateHistoryEnhancer): CreateHistory
useQueries(enhancer: CreateHistoryEnhancer): CreateHistory
useBasename<T>(createHistory: CreateHistory<T>): CreateHistory<T>
useBeforeUnload<T>(createHistory: CreateHistory<T>): CreateHistory<T & HistoryBeforeUnload>
useQueries<T>(createHistory: CreateHistory<T>): CreateHistory<T & HistoryQueries>
actions: {
PUSH: string
REPLACE: string
@@ -114,21 +124,21 @@ declare module "history/lib/createLocation" {
declare module "history/lib/useBasename" {
export default function useBasename(enhancer: HistoryModule.CreateHistoryEnhancer): HistoryModule.CreateHistory
export default function useBasename<T>(createHistory: HistoryModule.CreateHistory<T>): HistoryModule.CreateHistory<T>
}
declare module "history/lib/useBeforeUnload" {
export default function useBeforeUnload(enhancer: HistoryModule.CreateHistoryEnhancer): HistoryModule.CreateHistory
export default function useBeforeUnload<T>(createHistory: HistoryModule.CreateHistory<T>): HistoryModule.CreateHistory<T & HistoryModule.HistoryBeforeUnload>
}
declare module "history/lib/useQueries" {
export default function useQueries(enhancer: HistoryModule.CreateHistoryEnhancer): HistoryModule.CreateHistory
export default function useQueries<T>(createHistory: HistoryModule.CreateHistory<T>): HistoryModule.CreateHistory<T & HistoryModule.HistoryQueries>
}

View File

@@ -16,6 +16,8 @@ declare namespace ReactRouter {
type Component = React.ReactType
type Components = { [key: string]: Component }
type EnterHook = (nextState: RouterState, replaceState: RedirectFunction, callback?: Function) => any
type LeaveHook = () => any
@@ -47,24 +49,6 @@ declare namespace ReactRouter {
type StringifyQuery = (queryObject: H.Query) => H.QueryString
interface History extends H.HistoryBase {
pushState(state: H.LocationState, pathname: H.Pathname | H.Path, query?: H.Query): void
replaceState(state: H.LocationState, pathname: H.Pathname | H.Path, query?: H.Query): void
createPath(path: H.Path, query?: H.Query): H.Path
createHref(path: H.Path, query?: H.Query): H.Href
isActive(pathname: H.Pathname, query: H.Query): boolean
registerRouteHook(route: PlainRoute, hook: H.LocationListener): void
unregisterRouteHook(route: PlainRoute, hook: H.LocationListener): void
listen(listener: RouterListener): Function
match(location: H.Location, callback: (error: any, nextState: RouterState, nextLocation: H.Location) => void): void
routes: PlainRoute[]
parseQueryString?: ParseQueryString
stringifyQuery?: StringifyQuery
}
type RouterListener = (error: Error, nextState: RouterState) => void
interface RouterState {
location: Location
routes: RouteConfig
@@ -72,12 +56,18 @@ declare namespace ReactRouter {
components: Component[]
}
type RouteType = Route | IndexRoute | PlainRoute | Redirect
type RouteTypes = RouteType | RouteType[]
type Components = { [key: string]: Component }
interface HistoryBase extends H.History {
routes: PlainRoute[]
parseQueryString?: ParseQueryString
stringifyQuery?: StringifyQuery
}
type History = HistoryBase & H.HistoryQueries & HistoryRoutes
interface RouterProps {
@@ -179,10 +169,22 @@ declare namespace ReactRouter {
const History: React.Mixin<any, any>
function useRoutes(enhancer: H.CreateHistoryEnhancer): H.CreateHistory
type RouterListener = (error: Error, nextState: RouterState) => void
interface HistoryRoutes {
isActive(pathname: H.Pathname, query: H.Query): boolean
registerRouteHook(route: PlainRoute, hook: H.LocationListener): void
unregisterRouteHook(route: PlainRoute, hook: H.LocationListener): void
listen(listener: RouterListener): Function
match(location: H.Location, callback: (error: any, nextState: RouterState, nextLocation: H.Location) => void): void
}
function useRoutes<T>(createHistory: HistoryModule.CreateHistory<T>): HistoryModule.CreateHistory<T & HistoryRoutes>
function createRoutes(routes: RouteTypes): PlainRoute[]
interface MatchArgs {
routes?: RouteTypes
history?: H.History
@@ -345,6 +347,8 @@ declare module "react-router" {
import RouteContext from "react-router/lib/RouteContext"
import useRoutes from "react-router/lib/useRoutes"
import { createRoutes } from "react-router/lib/RouteUtils"
import RoutingContext from "react-router/lib/RoutingContext"
@@ -362,6 +366,7 @@ declare module "react-router" {
History,
Lifecycle,
RouteContext,
useRoutes,
createRoutes,
RoutingContext,
PropTypes,