[@types/history] History should be generic (#29024)

* Make exec / createRow methods generic

Saying they take/return Object is inaccurate, I wasn't sure if they should use object, any, or T extends object. I went with T extends object since it seems most correct.

* Update tests

* Update lint

* Replace generic with object from "Common Mistakes"

* Make History object generic

* Make History object generic

* Fix MemoryHistory definition, Fixed tested
This commit is contained in:
zackzeno
2018-10-11 14:55:48 -04:00
committed by Andy
parent abbce5d159
commit 52a012c362
3 changed files with 13 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
import { History, Location } from './index';
import { History, Location, LocationState } from './index';
import { getConfirmation } from './DOMUtils';
export interface MemoryHistoryBuildOptions {
@@ -8,9 +8,9 @@ export interface MemoryHistoryBuildOptions {
keyLength?: number;
}
export interface MemoryHistory extends History {
export interface MemoryHistory<HistoryLocationState = LocationState> extends History<HistoryLocationState> {
index: number;
entries: Location[];
entries: Location<HistoryLocationState>[];
canGo(n: number): boolean;
}

View File

@@ -1,4 +1,4 @@
import { createBrowserHistory, createMemoryHistory, createHashHistory, createLocation, Location } from 'history';
import { createBrowserHistory, createMemoryHistory, createHashHistory, createLocation, Location, History, MemoryHistory } from 'history';
import * as LocationUtils from 'history/LocationUtils';
import * as PathUtils from 'history/PathUtils';
import * as DOMUtils from 'history/DOMUtils';
@@ -7,7 +7,7 @@ import * as ExecutionEnvironment from 'history/ExecutionEnvironment';
let input = { value: "" };
{
let history = createBrowserHistory();
let history: History<{some: 'state'}> = createBrowserHistory();
// Listen for changes to the current location. The
// listener is called once immediately.
@@ -43,7 +43,7 @@ let input = { value: "" };
}
{
let history = createMemoryHistory();
let history: MemoryHistory<{the: 'state'}> = createMemoryHistory();
// Pushing a path string.
history.push('/the/path');

View File

@@ -8,20 +8,20 @@ export as namespace History;
export type Action = 'PUSH' | 'POP' | 'REPLACE';
export type UnregisterCallback = () => void;
export interface History {
export interface History<HistoryLocationState = LocationState> {
length: number;
action: Action;
location: Location;
push(path: Path, state?: LocationState): void;
push(location: LocationDescriptorObject): void;
replace(path: Path, state?: LocationState): void;
replace(location: LocationDescriptorObject): void;
location: Location<HistoryLocationState>;
push(path: Path, state?: HistoryLocationState): void;
push(location: LocationDescriptorObject<HistoryLocationState>): void;
replace(path: Path, state?: HistoryLocationState): void;
replace(location: LocationDescriptorObject<HistoryLocationState>): void;
go(n: number): void;
goBack(): void;
goForward(): void;
block(prompt?: boolean | string | TransitionPromptHook): UnregisterCallback;
listen(listener: LocationListener): UnregisterCallback;
createHref(location: LocationDescriptorObject): Href;
createHref(location: LocationDescriptorObject<HistoryLocationState>): Href;
}
export interface Location<S = LocationState> {