Skip to content

Commit 9b21163

Browse files
committed
fix(types): bind URL globals via module-scoped types like @types/node
Restructures the URL/URLSearchParams declarations after maintainer feedback to follow the @types/node web-globals pattern: shapes live in a module-scoped `url` namespace and are bound inside `declare global`. The global interfaces extend the module types (merging with lib.dom's) and the `var` declarations use `typeof globalThis extends { onmessage: any; URL: infer T } ? T : ...` so they adopt the type of any existing global definition instead of conflicting with it — no more TS2403 against lib.dom even without skipLibCheck.
1 parent 1d5bb88 commit 9b21163

1 file changed

Lines changed: 66 additions & 52 deletions

File tree

‎packages/types/globals.d.ts‎

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,76 @@
44
* URLSearchParamsImpl sources), so they resolve in projects compiled
55
* without the DOM lib.
66
*
7-
* The shapes intentionally mirror lib.dom.d.ts so the interfaces merge
8-
* cleanly when a project also includes the DOM lib. Members that the
9-
* runtimes do not implement (URL#toJSON, URL.parse, webkitURL, ...) are
10-
* omitted; the URL constructor requires a string first argument because
11-
* the runtimes throw a TypeError for anything else.
7+
* Follows the approach @types/node uses for web globals
8+
* (types/node/web-globals/url.d.ts): the shapes are declared at module
9+
* scope and bound inside `declare global`, so the global interfaces merge
10+
* with — and the `var` declarations adopt the type of — any existing
11+
* global definitions of URL/URLSearchParams (e.g. from lib.dom) instead
12+
* of conflicting with them.
13+
*
14+
* Members that the runtimes do not implement (URL#toJSON, URL.parse,
15+
* webkitURL, ...) are omitted; the URL constructor requires a string
16+
* first argument because the runtimes throw a TypeError for anything
17+
* else.
1218
*/
13-
interface URL {
14-
hash: string;
15-
host: string;
16-
hostname: string;
17-
href: string;
18-
readonly origin: string;
19-
password: string;
20-
pathname: string;
21-
port: string;
22-
protocol: string;
23-
search: string;
24-
readonly searchParams: URLSearchParams;
25-
username: string;
26-
toString(): string;
27-
}
19+
export {};
2820

29-
interface URLConstructor {
30-
new (url: string, base?: string | URL): URL;
31-
prototype: URL;
32-
canParse(url: string | URL, base?: string | URL): boolean;
33-
/**
34-
* Returns a `blob:nativescript/<uuid>` URL for a Blob/File, or null
35-
* when the value is not a Blob/File instance.
36-
*/
37-
createObjectURL(object: any, options?: { ext?: string } | null): string | null;
38-
revokeObjectURL(url: string): void;
39-
}
21+
declare namespace url {
22+
export interface URL {
23+
hash: string;
24+
host: string;
25+
hostname: string;
26+
href: string;
27+
readonly origin: string;
28+
password: string;
29+
pathname: string;
30+
port: string;
31+
protocol: string;
32+
search: string;
33+
readonly searchParams: URLSearchParams;
34+
username: string;
35+
toString(): string;
36+
}
4037

41-
declare var URL: URLConstructor;
38+
export interface URLConstructor {
39+
new (url: string, base?: string | URL): URL;
40+
prototype: URL;
41+
canParse(url: string | URL, base?: string | URL): boolean;
42+
/**
43+
* Returns a `blob:nativescript/<uuid>` URL for a Blob/File, or null
44+
* when the value is not a Blob/File instance.
45+
*/
46+
createObjectURL(object: any, options?: { ext?: string } | null): string | null;
47+
revokeObjectURL(url: string): void;
48+
}
4249

43-
interface URLSearchParams {
44-
readonly size: number;
45-
append(name: string, value: string): void;
46-
delete(name: string, value?: string): void;
47-
get(name: string): string | null;
48-
getAll(name: string): string[];
49-
has(name: string, value?: string): boolean;
50-
set(name: string, value: string): void;
51-
sort(): void;
52-
toString(): string;
53-
forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
54-
entries(): IterableIterator<[string, string]>;
55-
keys(): IterableIterator<string>;
56-
values(): IterableIterator<string>;
57-
[Symbol.iterator](): IterableIterator<[string, string]>;
58-
}
50+
export interface URLSearchParams {
51+
readonly size: number;
52+
append(name: string, value: string): void;
53+
delete(name: string, value?: string): void;
54+
get(name: string): string | null;
55+
getAll(name: string): string[];
56+
has(name: string, value?: string): boolean;
57+
set(name: string, value: string): void;
58+
sort(): void;
59+
toString(): string;
60+
forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
61+
entries(): IterableIterator<[string, string]>;
62+
keys(): IterableIterator<string>;
63+
values(): IterableIterator<string>;
64+
[Symbol.iterator](): IterableIterator<[string, string]>;
65+
}
5966

60-
interface URLSearchParamsConstructor {
61-
new (init?: string | string[][] | Record<string, string> | URLSearchParams | Iterable<readonly [string, string]>): URLSearchParams;
62-
prototype: URLSearchParams;
67+
export interface URLSearchParamsConstructor {
68+
new (init?: string | string[][] | Record<string, string> | URLSearchParams | Iterable<readonly [string, string]>): URLSearchParams;
69+
prototype: URLSearchParams;
70+
}
6371
}
6472

65-
declare var URLSearchParams: URLSearchParamsConstructor;
73+
declare global {
74+
interface URL extends url.URL {}
75+
var URL: typeof globalThis extends { onmessage: any; URL: infer T } ? T : url.URLConstructor;
76+
77+
interface URLSearchParams extends url.URLSearchParams {}
78+
var URLSearchParams: typeof globalThis extends { onmessage: any; URLSearchParams: infer T } ? T : url.URLSearchParamsConstructor;
79+
}

0 commit comments

Comments
 (0)