Skip to content

Commit d1ef5fb

Browse files
authored
[dom-navigation] Remove and fix types to match TS 6.0 (DefinitelyTyped#74546)
1 parent 7411f80 commit d1ef5fb

File tree

7 files changed

+286
-183
lines changed

7 files changed

+286
-183
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"@definitelytyped/no-type-only-packages": "off"
4+
}
5+
}

types/dom-navigation/index.d.ts

Lines changed: 2 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,3 @@
1-
interface Window {
2-
readonly navigation: Navigation;
3-
}
1+
// As of TS 6.0, the dom-navigation types are included in lib.dom.d.ts.
42

5-
interface NavigationEventMap {
6-
navigate: NavigateEvent;
7-
navigatesuccess: Event;
8-
navigateerror: ErrorEvent;
9-
currententrychange: NavigationCurrentEntryChangeEvent;
10-
}
11-
12-
interface NavigationResult {
13-
committed: Promise<NavigationHistoryEntry>;
14-
finished: Promise<NavigationHistoryEntry>;
15-
}
16-
17-
declare class Navigation extends EventTarget {
18-
entries(): NavigationHistoryEntry[];
19-
readonly currentEntry: NavigationHistoryEntry | null;
20-
updateCurrentEntry(options: NavigationUpdateCurrentEntryOptions): void;
21-
readonly transition: NavigationTransition | null;
22-
23-
readonly canGoBack: boolean;
24-
readonly canGoForward: boolean;
25-
26-
navigate(url: string, options?: NavigationNavigateOptions): NavigationResult;
27-
reload(options?: NavigationReloadOptions): NavigationResult;
28-
29-
traverseTo(key: string, options?: NavigationOptions): NavigationResult;
30-
back(options?: NavigationOptions): NavigationResult;
31-
forward(options?: NavigationOptions): NavigationResult;
32-
33-
onnavigate: ((this: Navigation, ev: NavigateEvent) => any) | null;
34-
onnavigatesuccess: ((this: Navigation, ev: Event) => any) | null;
35-
onnavigateerror: ((this: Navigation, ev: ErrorEvent) => any) | null;
36-
oncurrententrychange: ((this: Navigation, ev: NavigationCurrentEntryChangeEvent) => any) | null;
37-
38-
addEventListener<K extends keyof NavigationEventMap>(
39-
type: K,
40-
listener: (this: Navigation, ev: NavigationEventMap[K]) => any,
41-
options?: boolean | AddEventListenerOptions,
42-
): void;
43-
addEventListener(
44-
type: string,
45-
listener: EventListenerOrEventListenerObject,
46-
options?: boolean | AddEventListenerOptions,
47-
): void;
48-
removeEventListener<K extends keyof NavigationEventMap>(
49-
type: K,
50-
listener: (this: Navigation, ev: NavigationEventMap[K]) => any,
51-
options?: boolean | EventListenerOptions,
52-
): void;
53-
removeEventListener(
54-
type: string,
55-
listener: EventListenerOrEventListenerObject,
56-
options?: boolean | EventListenerOptions,
57-
): void;
58-
}
59-
60-
declare class NavigationTransition {
61-
readonly navigationType: NavigationTypeString;
62-
readonly from: NavigationHistoryEntry;
63-
readonly finished: Promise<void>;
64-
}
65-
66-
interface NavigationHistoryEntryEventMap {
67-
dispose: Event;
68-
}
69-
70-
interface NavigationHistoryEntry extends EventTarget {
71-
readonly key: string;
72-
readonly id: string;
73-
readonly url: string | null;
74-
readonly index: number;
75-
readonly sameDocument: boolean;
76-
77-
getState(): unknown;
78-
79-
ondispose: ((this: NavigationHistoryEntry, ev: Event) => any) | null;
80-
81-
addEventListener<K extends keyof NavigationHistoryEntryEventMap>(
82-
type: K,
83-
listener: (this: NavigationHistoryEntry, ev: NavigationHistoryEntryEventMap[K]) => any,
84-
options?: boolean | AddEventListenerOptions,
85-
): void;
86-
addEventListener(
87-
type: string,
88-
listener: EventListenerOrEventListenerObject,
89-
options?: boolean | AddEventListenerOptions,
90-
): void;
91-
removeEventListener<K extends keyof NavigationHistoryEntryEventMap>(
92-
type: K,
93-
listener: (this: NavigationHistoryEntry, ev: NavigationHistoryEntryEventMap[K]) => any,
94-
options?: boolean | EventListenerOptions,
95-
): void;
96-
removeEventListener(
97-
type: string,
98-
listener: EventListenerOrEventListenerObject,
99-
options?: boolean | EventListenerOptions,
100-
): void;
101-
}
102-
103-
declare var NavigationHistoryEntry: {
104-
prototype: NavigationHistoryEntry;
105-
new(): NavigationHistoryEntry;
106-
};
107-
108-
type NavigationTypeString = "reload" | "push" | "replace" | "traverse";
109-
110-
interface NavigationUpdateCurrentEntryOptions {
111-
state: unknown;
112-
}
113-
114-
interface NavigationOptions {
115-
info?: unknown;
116-
}
117-
118-
interface NavigationNavigateOptions extends NavigationOptions {
119-
state?: unknown;
120-
history?: "auto" | "push" | "replace";
121-
}
122-
123-
interface NavigationReloadOptions extends NavigationOptions {
124-
state?: unknown;
125-
}
126-
127-
declare class NavigationCurrentEntryChangeEvent extends Event {
128-
constructor(type: string, eventInit?: NavigationCurrentEntryChangeEventInit);
129-
130-
readonly navigationType: NavigationTypeString | null;
131-
readonly from: NavigationHistoryEntry;
132-
}
133-
134-
interface NavigationCurrentEntryChangeEventInit extends EventInit {
135-
navigationType?: NavigationTypeString | null;
136-
from: NavigationHistoryEntry;
137-
}
138-
139-
declare class NavigateEvent extends Event {
140-
constructor(type: string, eventInit?: NavigateEventInit);
141-
142-
readonly navigationType: NavigationTypeString;
143-
readonly canIntercept: boolean;
144-
readonly userInitiated: boolean;
145-
readonly hashChange: boolean;
146-
readonly hasUAVisualTransition: boolean;
147-
readonly destination: NavigationDestination;
148-
readonly signal: AbortSignal;
149-
readonly formData: FormData | null;
150-
readonly downloadRequest: string | null;
151-
readonly info?: unknown;
152-
153-
intercept(options?: NavigationInterceptOptions): void;
154-
scroll(): void;
155-
}
156-
157-
interface NavigateEventInit extends EventInit {
158-
navigationType?: NavigationTypeString;
159-
canIntercept?: boolean;
160-
userInitiated?: boolean;
161-
hashChange?: boolean;
162-
destination: NavigationDestination;
163-
signal: AbortSignal;
164-
formData?: FormData | null;
165-
downloadRequest?: string | null;
166-
info?: unknown;
167-
}
168-
169-
interface NavigationInterceptOptions {
170-
handler?: () => Promise<void>;
171-
focusReset?: "after-transition" | "manual";
172-
scroll?: "after-transition" | "manual";
173-
}
174-
175-
declare class NavigationDestination {
176-
readonly url: string;
177-
readonly key: string | null;
178-
readonly id: string | null;
179-
readonly index: number;
180-
readonly sameDocument: boolean;
181-
182-
getState(): unknown;
183-
}
3+
type NavigationTypeString = NavigationType;

types/dom-navigation/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
"projects": [
88
"https://wicg.github.io/navigation-api"
99
],
10+
"types": "index",
11+
"typesVersions": {
12+
"<=5.9": {
13+
"*": [
14+
"ts5.9/*"
15+
]
16+
}
17+
},
1018
"devDependencies": {
1119
"@types/dom-navigation": "workspace:."
1220
},
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const navigation = window.navigation;
2+
3+
const entries: NavigationHistoryEntry[] = navigation.entries();
4+
const entry: NavigationHistoryEntry | null = navigation.currentEntry;
5+
if (entry) {
6+
const key: string = entry.key;
7+
const id: string = entry.id;
8+
const url: string | null = entry.url;
9+
const index: number = entry.index;
10+
const sameDocument: boolean = entry.sameDocument;
11+
const state: unknown = entry.getState();
12+
entry.ondispose = () => {};
13+
const listener = () => {};
14+
entry.addEventListener("dispose", listener);
15+
entry.removeEventListener("dispose", listener);
16+
}
17+
navigation.updateCurrentEntry({ state: "" as unknown });
18+
const transition: NavigationTransition | null = navigation.transition;
19+
if (transition) {
20+
const navigationType: NavigationTypeString = transition.navigationType;
21+
const from: NavigationHistoryEntry = transition.from;
22+
const finished: Promise<void> = transition.finished;
23+
}
24+
const canGoBack: boolean = navigation.canGoBack;
25+
const canGoForward: boolean = navigation.canGoForward;
26+
navigation.navigate("/url", { state: "" as unknown, history: "replace", info: "" as unknown });
27+
navigation.reload({ state: "" as unknown, info: "" as unknown });
28+
navigation.traverseTo("", { info: "" as unknown });
29+
navigation.back({ info: "" as unknown });
30+
navigation.forward({ info: "" as unknown });
31+
const navigateListener = (e: NavigateEvent) => {
32+
const navigationType: NavigationTypeString = e.navigationType;
33+
const canIntercept: boolean = e.canIntercept;
34+
const userInitiated: boolean = e.userInitiated;
35+
const hashChange: boolean = e.hashChange;
36+
const hasUAVisualTransition: boolean = e.hasUAVisualTransition;
37+
const destination: NavigationDestination = e.destination;
38+
const url: string = destination.url;
39+
const key: string | null = destination.key;
40+
const id: string | null = destination.id;
41+
const index: number = destination.index;
42+
const sameDocument: boolean = destination.sameDocument;
43+
const state: unknown = destination.getState();
44+
const signal: AbortSignal = e.signal;
45+
const formData: FormData | null = e.formData;
46+
const downloadRequest: string | null = e.downloadRequest;
47+
const info: unknown = e.info;
48+
};
49+
navigation.onnavigate = navigateListener;
50+
navigation.addEventListener("navigate", navigateListener);
51+
navigation.removeEventListener("navigate", navigateListener);
52+
const navigateSuccessListener = (e: Event) => {};
53+
navigation.onnavigatesuccess = navigateSuccessListener;
54+
navigation.addEventListener("navigatesuccess", navigateSuccessListener);
55+
navigation.removeEventListener("navigatesuccess", navigateSuccessListener);
56+
const navigateErrorListener = (e: Event) => {};
57+
navigation.onnavigateerror = navigateErrorListener;
58+
navigation.addEventListener("navigateerror", navigateErrorListener);
59+
navigation.removeEventListener("navigateerror", navigateErrorListener);
60+
const currentEntryChangeListener = (e: NavigationCurrentEntryChangeEvent) => {
61+
const navigationType: NavigationTypeString | null = e.navigationType;
62+
const from: NavigationHistoryEntry = e.from;
63+
};
64+
navigation.oncurrententrychange = currentEntryChangeListener;
65+
navigation.addEventListener("currententrychange", currentEntryChangeListener);
66+
navigation.removeEventListener("currententrychange", currentEntryChangeListener);

0 commit comments

Comments
 (0)