Skip to content

Commit

Permalink
Client|Shared|Server: Consisting typing for RPC (#285)
Browse files Browse the repository at this point in the history
* Consisting typing for RPC

* Format fix
# Conflicts:
#	client/index.d.ts
  • Loading branch information
S0P4 authored and xLuxy committed Nov 7, 2023
1 parent dfc0cd7 commit c14ff70
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
9 changes: 6 additions & 3 deletions client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,8 @@ declare module "alt-client" {
* @remarks Exceptions will be thrown when there was an error on server-side.
*
*/
export function emitRpc(rpcName: string, ...args: unknown[]): Promise<unknown>;
export function emitRpc<K extends keyof shared.ICustomClientServerRpc>(rpcName: K, ...args: Parameters<shared.ICustomClientServerRpc[K]>): Promise<ReturnType<shared.ICustomClientServerRpc[K]>>;
export function emitRpc<K extends string>(rpcName: Exclude<K, keyof shared.ICustomClientServerRpc>, ...args: any[]): Promise<any>;

/**
* Subscribes to a client event with the specified listener.
Expand All @@ -2564,15 +2565,17 @@ declare module "alt-client" {
* @remarks The return value of the listener function determines the response clients will receive. When returning multiple values, use an array. Returning an Error object will cause the promise on the server to throw an exception which has to be caught.
*
*/
export function onRpc(rpcName: string, listener: (...args: unknown[]) => Promise<unknown> | unknown): void;
export function onRpc<K extends keyof shared.ICustomServerClientRpc>(rpcName: K, listener: (...args: Parameters<shared.ICustomServerClientRpc[K]>) => Promise<any> | ReturnType<any>): void;
export function onRpc<K extends string>(rpcName: Exclude<K, keyof shared.ICustomServerClientRpc>, listener: (...args: any[]) => Promise<any> | any): void;

/**
*
* @param rpcName Name of the RPC
* @param listener Listener that should be added.
*
*/
export function offRpc(rpcName: string, listener?: (player: Player, ...args: unknown[]) => Promise<unknown> | unknown): void;
export function offRpc<K extends keyof shared.ICustomServerClientRpc>(rpcName: K, listener?: (...args: Parameters<shared.ICustomServerClientRpc[K]>) => Promise<ReturnType<shared.ICustomServerClientRpc[K]>> | ReturnType<shared.ICustomServerClientRpc[K]>): void;
export function offRpc<K extends string>(rpcName: Exclude<K, keyof shared.ICustomServerClientRpc>, listener?: (...args: any[]) => Promise<any> | any): void;

/**
* Returns whether the game controls are currently enabled.
Expand Down
9 changes: 6 additions & 3 deletions server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,8 @@ declare module "alt-server" {
* @param ...args Arguments to pass to the RPC
*
*/
public emitRpc(rpcName: string, ...args: unknown[]): Promise<unknown>;
public emitRpc<K extends keyof shared.ICustomServerClientRpc>(rpcName: K, ...args: Parameters<shared.ICustomServerClientRpc[K]>): Promise<ReturnType<shared.ICustomServerClientRpc[K]>>;
public emitRpc<K extends string>(rpcName: Exclude<K, keyof shared.ICustomServerClientRpc>, ...args: any[]): Promise<any>;

public addWeaponComponent(weaponHash: number, component: number): void;

Expand Down Expand Up @@ -2990,15 +2991,17 @@ declare module "alt-server" {
* @remarks The return value of the listener function determines the response clients will receive. When returning multiple values, use an array. Returning an Error object will cause the promise on the client to throw an exception which has to be caught.
*
*/
export function onRpc(rpcName: string, listener: (player: Player, ...args: unknown[]) => Promise<unknown> | unknown): void;
export function onRpc<K extends keyof shared.ICustomClientServerRpc>(rpcName: K, listener: (player: Player, ...args: Parameters<shared.ICustomClientServerRpc[K]>) => Promise<ReturnType<shared.ICustomClientServerRpc[K]>> | ReturnType<shared.ICustomClientServerRpc[K]>): void;
export function onRpc<K extends string>(rpcName: Exclude<K, keyof shared.ICustomClientServerRpc>, listener: (player: Player, ...args: any[]) => Promise<any> | any): void;

/**
*
* @param rpcName Name of the RPC
* @param listener Listener that should be added.
*
*/
export function offRpc(rpcName: string, listener?: (player: Player, ...args: unknown[]) => Promise<unknown> | unknown): void;
export function offRpc<K extends keyof shared.ICustomClientServerRpc>(rpcName: string, listener: (player: Player, ...args: Parameters<shared.ICustomClientServerRpc[K]>) => Promise<ReturnType<shared.ICustomClientServerRpc[K]>> | ReturnType<shared.ICustomClientServerRpc[K]>): void;
export function offRpc<K extends string>(rpcName: Exclude<K, keyof shared.ICustomClientServerRpc>, listener?: (player: Player, ...args: any[]) => Promise<any> | any): void;

/**
* Change the server password at runtime.
Expand Down
34 changes: 34 additions & 0 deletions shared/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,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<boolean>
* }
* }
* ```
*
* @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<boolean>
* }
* }
* ```
*
* @export
* @interface ICustomClientServerRpc
*/
export interface ICustomClientServerRpc {}

export interface IInspectOptions {
/**
* If set to `true`, getters are going to be
Expand Down

0 comments on commit c14ff70

Please sign in to comment.