|
4 | 4 | * URLSearchParamsImpl sources), so they resolve in projects compiled |
5 | 5 | * without the DOM lib. |
6 | 6 | * |
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. |
12 | 18 | */ |
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 {}; |
28 | 20 |
|
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 | + } |
40 | 37 |
|
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 | + } |
42 | 49 |
|
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 | + } |
59 | 66 |
|
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 | + } |
63 | 71 | } |
64 | 72 |
|
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