mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 14:20:12 +00:00
History enhancers definitions improved using union types
This commit is contained in:
42
react-router/history.d.ts
vendored
42
react-router/history.d.ts
vendored
@@ -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>
|
||||
|
||||
}
|
||||
|
||||
|
||||
47
react-router/react-router.d.ts
vendored
47
react-router/react-router.d.ts
vendored
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user