From b9c14638e9343812120cddb6c937f3ee831cae6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20LES=C3=89N=C3=89CHAL?= Date: Mon, 29 Jan 2024 18:36:13 +0100 Subject: [PATCH] Fix some type issues from oversights or revisions by @AnYiEE --- jquery/textSelection.d.ts | 2 +- mw/Api.d.ts | 32 +++++++++++++++----------------- mw/ForeignApi.d.ts | 6 +++--- mw/ForeignRest.d.ts | 10 +++++----- mw/Map.d.ts | 2 +- mw/Title.d.ts | 4 ++-- mw/Uri.d.ts | 2 +- mw/config.d.ts | 2 +- mw/cookie.d.ts | 9 +++++---- mw/index.d.ts | 15 ++++++++++++++- mw/loader.d.ts | 2 +- mw/user.d.ts | 4 ++-- mw/util.d.ts | 19 +++++++++---------- 13 files changed, 60 insertions(+), 49 deletions(-) diff --git a/jquery/textSelection.d.ts b/jquery/textSelection.d.ts index 93d24c5..ecf808f 100644 --- a/jquery/textSelection.d.ts +++ b/jquery/textSelection.d.ts @@ -134,7 +134,7 @@ declare global { * @returns {Mixed} Depending on the command * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/jQuery.plugin.textSelection-method-textSelection */ - textSelection(command: string, commandOptions?: any): void; + textSelection(command: string, commandOptions?: unknown): any; } } diff --git a/mw/Api.d.ts b/mw/Api.d.ts index 5154ba2..416d9e6 100644 --- a/mw/Api.d.ts +++ b/mw/Api.d.ts @@ -8,7 +8,9 @@ import { import { TitleLike } from "./Title"; import { UserInfo } from "./user"; -type TitleLikeArray = string[] | mw.Title[]; // TitleLike[] would be a mixed array +type TypeOrArray = T extends any ? T | T[] : never; // T[] would be a mixed array +type ReplaceValue = T extends U[] ? V[] : V; + type ApiParams = Record; type ApiResponse = Record; // it will always be a JSON object, the rest is uncertain ... @@ -22,11 +24,11 @@ export interface ApiOptions { /** * Default query parameters for API requests */ - parameters?: ApiParams; + parameters: ApiParams; /** * Default options for {@link jQuery.ajax} */ - ajax?: JQuery.AjaxSettings; + ajax: JQuery.AjaxSettings; /** * Whether to use U+001F when joining multi-valued parameters (since 1.28). * Default is true if ajax.url is not set, false otherwise for compatibility. @@ -70,7 +72,7 @@ declare global { * @param {ApiOptions} [options] * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api-method-constructor */ - constructor(options?: ApiOptions); + constructor(options?: Partial); private defaults: ApiOptions; @@ -405,34 +407,30 @@ declare global { /** * Convenience method for `action=watch`. * - * @param {TitleLike | TitleLikeArray} pages + * @param {TypeOrArray} pages * @param {string} [expiry] - * @returns {JQuery.Promise<{ watch: { title: string, watched: boolean } | Array<{ title: string, watched: boolean }> }>} + * @returns {JQuery.Promise<{ watch: TypeOrArray<{ title: string, watched: boolean }> }>} * @since 1.35: expiry parameter can be passed when watchlist expiry is enabled * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api.plugin.watch-method-watch */ - watch

( + watch

>( pages: P, expiry?: string ): JQuery.Promise<{ - watch: P extends TitleLikeArray - ? Array<{ title: string; watched: boolean }> - : { title: string; watched: boolean }; + watch: ReplaceValue; }>; /** * Convenience method for `action=watch&unwatch=1`. * - * @param {TitleLike | TitleLikeArray} pages - * @returns {JQuery.Promise<{ watch: { title: string, watched: boolean } | Array<{ title: string, watched: boolean }> }>} + * @param {TypeOrArray} pages + * @returns {JQuery.Promise<{ watch: TypeOrArray<{ title: string, watched: boolean }> }>} * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api.plugin.watch-method-unwatch */ - unwatch

( + unwatch

>( pages: P ): JQuery.Promise<{ - watch: P extends TitleLikeArray - ? Array<{ title: string; watched: boolean }> - : { title: string; watched: boolean }; + watch: ReplaceValue; }>; /** @@ -559,7 +557,7 @@ declare global { * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api.plugin.upload-method-chunkedUploadToStash */ chunkedUploadToStash( - file: File, + file: File | HTMLInputElement, data?: ApiUploadParams, chunkSize?: number, chunkRetries?: number diff --git a/mw/ForeignApi.d.ts b/mw/ForeignApi.d.ts index 4577a2c..0f04cdd 100644 --- a/mw/ForeignApi.d.ts +++ b/mw/ForeignApi.d.ts @@ -4,7 +4,7 @@ interface ForeignApiOptions extends ApiOptions { /** * Whether to perform all requests anonymously. Use this option if the target wiki may otherwise not accept cross-origin requests, or if you don't need to perform write actions or read restricted information and want to avoid the overhead. */ - anonymous?: boolean; + anonymous: boolean; } declare global { @@ -39,11 +39,11 @@ declare global { * ``` * * @param {string | Uri} url URL pointing to another wiki's `api.php` endpoint. - * @param {ForeignApiOptions} [options] + * @param {Partial} [options] * @since 1.26 * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.ForeignApi-method-constructor */ - constructor(url: string | Uri, options?: ForeignApiOptions); + constructor(url: string | Uri, options?: Partial); /** * Return the origin to use for API requests, in the required format (protocol, host and port, if any). diff --git a/mw/ForeignRest.d.ts b/mw/ForeignRest.d.ts index 7638b36..cf327f1 100644 --- a/mw/ForeignRest.d.ts +++ b/mw/ForeignRest.d.ts @@ -6,7 +6,7 @@ interface ForeignRestOptions extends RestOptions { * accept cross-origin requests, or if you don't need to perform write actions or read * restricted information and want to avoid the overhead. */ - anonymous?: boolean; + anonymous: boolean; } declare global { @@ -41,16 +41,16 @@ declare global { * mw.ForeignRest = MyForeignRest; * ``` * - * @param {string | Uri} url URL pointing to another wiki's rest.php endpoint. + * @param {string} url URL pointing to another wiki's `rest.php` endpoint. * @param {ForeignApi} foreignActionApi - * @param {ForeignRestOptions} [options] + * @param {Partial} [options] * @since 1.36 * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.ForeignApi-method-constructor */ constructor( - url: string | Uri, + url: string, foreignActionApi: ForeignApi, - options?: ForeignRestOptions + options?: Partial ); } } diff --git a/mw/Map.d.ts b/mw/Map.d.ts index dbf9e8d..3f5708b 100644 --- a/mw/Map.d.ts +++ b/mw/Map.d.ts @@ -34,7 +34,7 @@ declare global { */ get(selection: S[], fallback?: any): Pick; get(selection: S, fallback?: V[S]): V[S]; - get(): V; + get(): S; /** * Set the value of one or more keys. diff --git a/mw/Title.d.ts b/mw/Title.d.ts index 2b55b07..c52b282 100644 --- a/mw/Title.d.ts +++ b/mw/Title.d.ts @@ -344,7 +344,7 @@ declare global { * @returns {Title|null} The file title or null if unsuccessful * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Title-static-method-newFromImg */ - static newFromImg(img: HTMLElement | JQuery): Title; + static newFromImg(img: HTMLElement | JQuery): Title | null; /** * Constructor for Title objects with a null return instead of an exception for invalid titles. @@ -378,7 +378,7 @@ declare global { static newFromUserInput( title: string, defaultNamespace?: number, - options?: { forUploading: boolean } + options?: { forUploading?: boolean } ): Title | null; /** diff --git a/mw/Uri.d.ts b/mw/Uri.d.ts index d2f3a48..09d786f 100644 --- a/mw/Uri.d.ts +++ b/mw/Uri.d.ts @@ -1,4 +1,4 @@ -export type QueryParams = Record; +export type QueryParams = Record; interface UriOptions { /** diff --git a/mw/config.d.ts b/mw/config.d.ts index e3ad45c..9c32d62 100644 --- a/mw/config.d.ts +++ b/mw/config.d.ts @@ -19,7 +19,7 @@ declare global { * * @see https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#debug */ - debug: number; + debug: boolean | number; /** * The internal name of the currently used skin. * diff --git a/mw/cookie.d.ts b/mw/cookie.d.ts index 02feee3..3171d9d 100644 --- a/mw/cookie.d.ts +++ b/mw/cookie.d.ts @@ -22,7 +22,7 @@ declare global { prefix: string | undefined | null, defaultValue: D ): string | D; - function get(key: string, prefix?: string): string; + function get(key: string, prefix?: string | null): string; /** * Get the value of a `SameSite` = `None` cookie, using the legacy `ss0-` prefix if needed. @@ -38,7 +38,7 @@ declare global { prefix: string | undefined | null, defaultValue: D ): string | D; - function getCrossSite(key: string, prefix?: string): string; + function getCrossSite(key: string, prefix?: string | null): string; /** * Set or delete a cookie. @@ -64,16 +64,17 @@ declare global { // see https://stackoverflow.com/a/64932909 for function set( key: string, - value: any, + value: string | null, options?: | Date | number | Partial<{ domain: string; - expires: Date | number; + expires: Date | number | null; path: string; prefix: string; sameSite: Lowercase extends SameSite ? SS : SameSite; + sameSiteLegacy: boolean; secure: boolean; }> ): void; diff --git a/mw/index.d.ts b/mw/index.d.ts index 9c68b8f..31d5e0c 100644 --- a/mw/index.d.ts +++ b/mw/index.d.ts @@ -44,7 +44,7 @@ declare global { * want to add a new global, or the global is bad and needs containment * or wrapping. */ - const libs: any; + const libs: Record; /** * OOUI widgets specific to MediaWiki @@ -176,6 +176,19 @@ declare global { * @param {Function} callback */ function trackUnsubscribe(callback: (topic: string, data: object) => void): void; + + /** + * List of all analytic events emitted so far. + * + * Exposed only for use by mediawiki.base. + * + * @private + * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw-property-trackQueue + */ + const trackQueue: Array<{ + topic: string; + data: Record | number | string | undefined; + }>; } } diff --git a/mw/loader.d.ts b/mw/loader.d.ts index dccb866..ae416c2 100644 --- a/mw/loader.d.ts +++ b/mw/loader.d.ts @@ -217,7 +217,7 @@ declare global { */ function implement( module: string, - script?: any, + script?: ModuleScript, style?: ModuleStyle, messages?: ModuleMessages, templates?: ModuleTemplates, diff --git a/mw/user.d.ts b/mw/user.d.ts index 9312e35..7dc3474 100644 --- a/mw/user.d.ts +++ b/mw/user.d.ts @@ -72,7 +72,7 @@ export interface User { * @returns {JQuery.Promise} * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.user-method-getGroups */ - getGroups(callback: (groups: string[]) => any): JQuery.Promise; + getGroups(callback: (groups: string[]) => T): JQuery.Promise; getGroups(): JQuery.Promise; /** @@ -119,7 +119,7 @@ export interface User { * @returns {JQuery.Promise} * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.user-method-getRights */ - getRights(callback: (rights: string[]) => any): JQuery.Promise; + getRights(callback: (rights: string[]) => T): JQuery.Promise; getRights(): JQuery.Promise; /** diff --git a/mw/util.d.ts b/mw/util.d.ts index d0fb872..a68efb7 100644 --- a/mw/util.d.ts +++ b/mw/util.d.ts @@ -1,3 +1,5 @@ +import { QueryParams } from "./Uri"; + type NoReturn any> = T extends ( this: infer U, ...args: infer V @@ -296,17 +298,14 @@ declare global { * Get the URL to a given local wiki page name, * * @param {string|null} [pageName=wgPageName] Page name - * @param {Object} [params] A mapping of query parameter names to values, + * @param {QueryParams} [params] A mapping of query parameter names to values, * e.g. `{ action: 'edit' }` * @returns {string} URL, relative to `wgServer`. * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.util-method-getUrl */ // params are handled by $.param, which converts any value to a string. However, instead of using toString(), // object are serialized (deep ones recursively), so only simple values are allowed to prevent mistakes. - function getUrl( - pageName?: string | null, - params?: { [param: string]: string | number | boolean | null | undefined } - ): string; + function getUrl(pageName?: string | null, params?: QueryParams): string; /** * Hide a portlet. @@ -347,14 +346,14 @@ declare global { * @returns {boolean} * @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.util-method-isIPv4Address */ - function isIPv4Address( - address: string, - allowBlock: true - ): address is `${number}.${number}.${number}.${number}${`/${number}` | ""}`; function isIPv4Address( address: string, allowBlock?: false ): address is `${number}.${number}.${number}.${number}`; + function isIPv4Address( + address: string, + allowBlock: boolean + ): address is `${number}.${number}.${number}.${number}${`/${number}` | ""}`; /** * Whether a string is a valid IPv6 address or not. @@ -423,7 +422,7 @@ declare global { ): { name: string; width?: number | null; - resizeUrl: (w: number) => string; + resizeUrl(w: number): string; } | null; /**