UNPKG

4.12 kBTypeScriptView Raw
1// Type definitions for history 4.7.2
2// Project: https://github.com/mjackson/history
3// Definitions by: Sergey Buturlakin <https://github.com/sergey-buturlakin>, Nathan Brown <https://github.com/ngbrown>, Young Rok Kim <https://github.com/rokoroku>, Daniel Nixon <https://github.com/danielnixon>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.3
6export as namespace History;
7
8export type Action = 'PUSH' | 'POP' | 'REPLACE';
9export type UnregisterCallback = () => void;
10
11export interface History<HistoryLocationState = LocationState> {
12 length: number;
13 action: Action;
14 location: Location<HistoryLocationState>;
15 push(path: Path, state?: HistoryLocationState): void;
16 push(location: LocationDescriptor<HistoryLocationState>): void;
17 replace(path: Path, state?: HistoryLocationState): void;
18 replace(location: LocationDescriptor<HistoryLocationState>): void;
19 go(n: number): void;
20 goBack(): void;
21 goForward(): void;
22 block(prompt?: boolean | string | TransitionPromptHook<HistoryLocationState>): UnregisterCallback;
23 listen(listener: LocationListener<HistoryLocationState>): UnregisterCallback;
24 createHref(location: LocationDescriptorObject<HistoryLocationState>): Href;
25}
26
27export interface Location<S = LocationState> {
28 pathname: Pathname;
29 search: Search;
30 state: S;
31 hash: Hash;
32 key?: LocationKey;
33}
34
35export interface LocationDescriptorObject<S = LocationState> {
36 pathname?: Pathname;
37 search?: Search;
38 state?: S;
39 hash?: Hash;
40 key?: LocationKey;
41}
42
43export namespace History {
44 export type LocationDescriptor<S = LocationState> = Path | LocationDescriptorObject<S>;
45 export type LocationKey = string;
46 export type LocationListener<S = LocationState> = (location: Location<S>, action: Action) => void;
47
48 export type LocationState = unknown;
49 export type Path = string;
50 export type Pathname = string;
51 export type Search = string;
52 export type TransitionHook<S = LocationState> = (location: Location<S>, callback: (result: any) => void) => any;
53 export type TransitionPromptHook<S = LocationState> = (
54 location: Location<S>,
55 action: Action,
56 ) => string | false | void;
57 export type Hash = string;
58 export type Href = string;
59}
60
61export type LocationDescriptor<S = LocationState> = History.LocationDescriptor<S>;
62export type LocationKey = History.LocationKey;
63export type LocationListener<S = LocationState> = History.LocationListener<S>;
64export type LocationState = History.LocationState;
65export type Path = History.Path;
66export type Pathname = History.Pathname;
67export type Search = History.Search;
68export type TransitionHook<S = LocationState> = History.TransitionHook<S>;
69export type TransitionPromptHook<S = LocationState> = History.TransitionPromptHook<S>;
70export type Hash = History.Hash;
71export type Href = History.Href;
72
73import { default as createBrowserHistory } from './createBrowserHistory';
74import { default as createHashHistory } from './createHashHistory';
75import { default as createMemoryHistory } from './createMemoryHistory';
76import { createLocation, locationsAreEqual } from './LocationUtils';
77import { parsePath, createPath } from './PathUtils';
78
79// Global usage, without modules, needs the small trick, because lib.d.ts
80// already has `history` and `History` global definitions:
81// var createHistory = ((window as any).History as HistoryModule.Module).createHistory;
82export interface Module {
83 createBrowserHistory: typeof createBrowserHistory;
84 createHashHistory: typeof createHashHistory;
85 createMemoryHistory: typeof createMemoryHistory;
86 createLocation: typeof createLocation;
87 locationsAreEqual: typeof locationsAreEqual;
88 parsePath: typeof parsePath;
89 createPath: typeof createPath;
90}
91
92export * from './createBrowserHistory';
93export * from './createHashHistory';
94export * from './createMemoryHistory';
95export { createLocation, locationsAreEqual } from './LocationUtils';
96export { parsePath, createPath } from './PathUtils';
97export { createBrowserHistory, createHashHistory, createMemoryHistory };