Skip to content

Commit

Permalink
Fix some type issues
Browse files Browse the repository at this point in the history
from oversights or revisions by @AnYiEE
  • Loading branch information
Adrien LESÉNÉCHAL committed Jan 29, 2024
1 parent 64e562b commit b9c1463
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 49 deletions.
2 changes: 1 addition & 1 deletion jquery/textSelection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
32 changes: 15 additions & 17 deletions mw/Api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = T extends any ? T | T[] : never; // T[] would be a mixed array
type ReplaceValue<T extends U | U[], U, V> = T extends U[] ? V[] : V;

type ApiParams = Record<string, string | string[] | boolean | number | number[]>;
type ApiResponse = Record<string, any>; // it will always be a JSON object, the rest is uncertain ...

Expand All @@ -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.
Expand Down Expand Up @@ -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<ApiOptions>);

private defaults: ApiOptions;

Expand Down Expand Up @@ -405,34 +407,30 @@ declare global {
/**
* Convenience method for `action=watch`.
*
* @param {TitleLike | TitleLikeArray} pages
* @param {TypeOrArray<TitleLike>} 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<P extends TitleLike | TitleLikeArray>(
watch<P extends TypeOrArray<TitleLike>>(
pages: P,
expiry?: string
): JQuery.Promise<{
watch: P extends TitleLikeArray
? Array<{ title: string; watched: boolean }>
: { title: string; watched: boolean };
watch: ReplaceValue<P, TitleLike, { title: string; watched: boolean }>;
}>;

/**
* 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<TitleLike>} 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<P extends TitleLike | TitleLikeArray>(
unwatch<P extends TypeOrArray<TitleLike>>(
pages: P
): JQuery.Promise<{
watch: P extends TitleLikeArray
? Array<{ title: string; watched: boolean }>
: { title: string; watched: boolean };
watch: ReplaceValue<P, TitleLike, { title: string; watched: boolean }>;
}>;

/**
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions mw/ForeignApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -39,11 +39,11 @@ declare global {
* ```
*
* @param {string | Uri} url URL pointing to another wiki's `api.php` endpoint.
* @param {ForeignApiOptions} [options]
* @param {Partial<ForeignApiOptions>} [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<ForeignApiOptions>);

/**
* Return the origin to use for API requests, in the required format (protocol, host and port, if any).
Expand Down
10 changes: 5 additions & 5 deletions mw/ForeignRest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ForeignRestOptions>} [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<ForeignRestOptions>
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion mw/Map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare global {
*/
get<S extends keyof V>(selection: S[], fallback?: any): Pick<V, S>;
get<S extends keyof V>(selection: S, fallback?: V[S]): V[S];
get(): V;
get<S extends V = V>(): S;

/**
* Set the value of one or more keys.
Expand Down
4 changes: 2 additions & 2 deletions mw/Title.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -378,7 +378,7 @@ declare global {
static newFromUserInput(
title: string,
defaultNamespace?: number,
options?: { forUploading: boolean }
options?: { forUploading?: boolean }
): Title | null;

/**
Expand Down
2 changes: 1 addition & 1 deletion mw/Uri.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type QueryParams = Record<string, any>;
export type QueryParams = Record<string, string | number | boolean | null | undefined>;

interface UriOptions {
/**
Expand Down
2 changes: 1 addition & 1 deletion mw/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 5 additions & 4 deletions mw/cookie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -64,16 +64,17 @@ declare global {
// see https://stackoverflow.com/a/64932909 for <SS>
function set<SS extends string = SameSite>(
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<SS> extends SameSite ? SS : SameSite;
sameSiteLegacy: boolean;
secure: boolean;
}>
): void;
Expand Down
15 changes: 14 additions & 1 deletion mw/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;

/**
* OOUI widgets specific to MediaWiki
Expand Down Expand Up @@ -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<string, any> | number | string | undefined;
}>;
}
}

Expand Down
2 changes: 1 addition & 1 deletion mw/loader.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ declare global {
*/
function implement(
module: string,
script?: any,
script?: ModuleScript,
style?: ModuleStyle,
messages?: ModuleMessages,
templates?: ModuleTemplates,
Expand Down
4 changes: 2 additions & 2 deletions mw/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<undefined>;
getGroups<T>(callback: (groups: string[]) => T): JQuery.Promise<T>;
getGroups(): JQuery.Promise<string[]>;

/**
Expand Down Expand Up @@ -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<undefined>;
getRights<T>(callback: (rights: string[]) => T): JQuery.Promise<T>;
getRights(): JQuery.Promise<string[]>;

/**
Expand Down
19 changes: 9 additions & 10 deletions mw/util.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { QueryParams } from "./Uri";

type NoReturn<T extends (...args: any[]) => any> = T extends (
this: infer U,
...args: infer V
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -423,7 +422,7 @@ declare global {
): {
name: string;
width?: number | null;
resizeUrl: (w: number) => string;
resizeUrl(w: number): string;
} | null;

/**
Expand Down

0 comments on commit b9c1463

Please sign in to comment.