From ad3530e535ce33ba9ebb8c69177ca9ff4e48e7f7 Mon Sep 17 00:00:00 2001 From: S0P4 Date: Sun, 5 Nov 2023 17:38:18 -0300 Subject: [PATCH] Client|Shared|Server: Consisting typing for RPC --- client/index.d.ts | 9 ++++++--- server/index.d.ts | 11 +++++++---- shared/index.d.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/client/index.d.ts b/client/index.d.ts index 3ae41bad..a3a1554a 100644 --- a/client/index.d.ts +++ b/client/index.d.ts @@ -2779,7 +2779,8 @@ declare module "alt-client" { * * @beta */ - export function emitRpc(rpcName: string, ...args: unknown[]): Promise; + export function emitRpc(rpcName: K, ...args: Parameters): Promise>; + export function emitRpc(rpcName: Exclude, ...args: any[]): Promise; /** * Subscribes to a client event with the specified listener. @@ -2790,7 +2791,8 @@ declare module "alt-client" { * * @beta */ - export function onRpc(rpcName: string, listener: (player: Player, ...args: unknown[]) => Promise | unknown): void; + export function onRpc(rpcName: K, listener: (...args: Parameters) => Promise | ReturnType): void; + export function onRpc(rpcName: Exclude, listener: (...args: any[]) => Promise | any): void; /** * @@ -2799,7 +2801,8 @@ declare module "alt-client" { * * @beta */ - export function offRpc(rpcName: string, listener?: (player: Player, ...args: unknown[]) => Promise | unknown): void; + export function offRpc(rpcName: K, listener?: (...args: Parameters) => Promise> | ReturnType): void; + export function offRpc(rpcName: Exclude, listener?: (...args: any[]) => Promise | any): void; /** * Returns whether the game controls are currently enabled. diff --git a/server/index.d.ts b/server/index.d.ts index f8041e43..68bc9db6 100644 --- a/server/index.d.ts +++ b/server/index.d.ts @@ -1181,7 +1181,8 @@ declare module "alt-server" { * * @beta */ - public emitRpc(rpcName: string, ...args: unknown[]): Promise; + public emitRpc(rpcName: K, ...args: Parameters): Promise>; + public emitRpc(rpcName: Exclude, ...args: any[]): Promise; public addWeaponComponent(weaponHash: number, component: number): void; @@ -3145,7 +3146,8 @@ declare module "alt-server" { * * @beta */ - export function onRpc(rpcName: string, listener: (player: Player, ...args: unknown[]) => Promise | unknown): void; + export function onRpc(rpcName: K, listener: (player: Player, ...args: Parameters) => Promise> | ReturnType): void; + export function onRpc(rpcName: Exclude, listener: (player: Player, ...args: any[]) => Promise | any): void; /** * @@ -3153,8 +3155,9 @@ declare module "alt-server" { * @param listener Listener that should be added. * * @beta - */ - export function offRpc(rpcName: string, listener?: (player: Player, ...args: unknown[]) => Promise | unknown): void; + */ + export function offRpc(rpcName: string, listener: (player: Player, ...args: Parameters) => Promise> | ReturnType): void; + export function offRpc(rpcName: Exclude, listener?: (player: Player, ...args: any[]) => Promise | any): void; /** * Change the server password at runtime. diff --git a/shared/index.d.ts b/shared/index.d.ts index a4dadc2a..c8df9f9f 100644 --- a/shared/index.d.ts +++ b/shared/index.d.ts @@ -1933,6 +1933,40 @@ declare module "alt-shared" { */ export interface ICustomClientServerEvent {} + /** + * Extend `player.emitRpc` and `alt.onRpc` auto-completion by merging interfaces. + * + * @example + * ```ts + * declare module 'alt-client' { + * interface ICustomServerClientRpc { + * myRpc: (arg1: string, arg2: { key: string, value: number }): Promise + * } + * } + * ``` + * + * @export + * @interface ICustomServerClientRpc + */ + export interface ICustomServerClientRpc {} + + /** + * Extend `alt.onRpc` and `alt.emitRpc` auto-completion by merging interfaces. + * + * @example + * ```ts + * declare module 'alt-client' { + * interface ICustomClientServerRpc { + * myRpc: (arg1: string, arg2: { key: string, value: number }): Promise + * } + * } + * ``` + * + * @export + * @interface ICustomClientServerRpc + */ + export interface ICustomClientServerRpc {} + export interface IInspectOptions { /** * If set to `true`, getters are going to be