From 0d64ea1b82ad1d804892ceec14d98a9be85f64fb Mon Sep 17 00:00:00 2001 From: Sergey Buturlakin Date: Thu, 1 Oct 2015 12:31:03 +0300 Subject: [PATCH] History enhancers definitions improved using union types --- react-router/history.d.ts | 42 ++++++++++++++++++------------ react-router/react-router.d.ts | 47 +++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/react-router/history.d.ts b/react-router/history.d.ts index c982bd2299..2d88184e68 100644 --- a/react-router/history.d.ts +++ b/react-router/history.d.ts @@ -12,12 +12,13 @@ declare namespace HistoryModule { type BeforeUnloadHook = () => string - type CreateHistory = (options?: HistoryOptions) => History + type CreateHistory = (options?: HistoryOptions) => T - type CreateHistoryEnhancer = (createHistory: CreateHistory) => CreateHistory + type CreateHistoryEnhancer = (createHistory: CreateHistory) => CreateHistory - 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 + createHashHistory: CreateHistory + createMemoryHistory: CreateHistory createLocation(): Location - useBasename(enhancer: CreateHistoryEnhancer): CreateHistory - useBeforeUnload(enhancer: CreateHistoryEnhancer): CreateHistory - useQueries(enhancer: CreateHistoryEnhancer): CreateHistory + useBasename(createHistory: CreateHistory): CreateHistory + useBeforeUnload(createHistory: CreateHistory): CreateHistory + useQueries(createHistory: CreateHistory): CreateHistory 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(createHistory: HistoryModule.CreateHistory): HistoryModule.CreateHistory } declare module "history/lib/useBeforeUnload" { - export default function useBeforeUnload(enhancer: HistoryModule.CreateHistoryEnhancer): HistoryModule.CreateHistory + export default function useBeforeUnload(createHistory: HistoryModule.CreateHistory): HistoryModule.CreateHistory } declare module "history/lib/useQueries" { - export default function useQueries(enhancer: HistoryModule.CreateHistoryEnhancer): HistoryModule.CreateHistory + export default function useQueries(createHistory: HistoryModule.CreateHistory): HistoryModule.CreateHistory } diff --git a/react-router/react-router.d.ts b/react-router/react-router.d.ts index 3869978764..0834954048 100644 --- a/react-router/react-router.d.ts +++ b/react-router/react-router.d.ts @@ -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 - 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(createHistory: HistoryModule.CreateHistory): HistoryModule.CreateHistory + 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,